我从MatLab开始。
我创建了一个指南用户界面。我继续前进,并假设我可以添加新的feild到创建的句柄结构。
我可以这样做吗?
例如,当我在程序中引用handle.s时,我收到一个错误,我正在向一个不存在的feild提供。
创建了handles.s来存储在函数中生成的串行端口对象,该函数初始化我的PC和微控制器之间的串行通信。
串口对象有自己的方法和方法...可能是因为我无法将对象作为feild传递给包含指南UI属性的handle对象吗?
以下是我正在使用的代码
function [ s, flag] = setupSerial(comPort)
%Initialize serial port communication between Arduino and Matlab
%Ensure that the arduino is also communicating with Matlab at this time.
%if setup is complete then the value of setup is returned as 1 else 0.
flag =1;
s = serial(comPort);
set(s,'DataBits',8);
set(s,'StopBits',1);
set(s,'BaudRate',9600);
set(s,'Parity','none');
fopen(s);
a='b';
while (a~='a')
a=fread(s,1,'uchar');
end
if (a=='a')
disp('serial read');
end
fprintf(s,'%c','a');
mbox = msgbox('Serial Communication setup.'); uiwait(mbox);
fscanf(s,'%u');
end
在我的UserInterface.m文件中,我在claaback函数中传递对象,如下所示
function SerialBtn_Callback(hObject, eventdata, handles)
% hObject handle to SerialBtn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA
comPort = get(handles.COMportTxt,'String');
if(~exist('serialFlag','var'))
[handles.s, handles.serialFlag] = setupSerial(comPort);
end
end
当我按下' Home'按钮。这是回调功能
function HomeButton_Callback(hObject, eventdata, handles)
% hObject handle to HomeButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
disp('home button pressed');
fprintf(handles.s,'%s', 'G2');
set(handles.CurrentPositionTxt, 'String', '0');
end
我得到的错误是以下
Reference to non-existent field 's'.
这里是您的GUI信息
答案 0 :(得分:1)
定义handle.s的回调函数不保存handle对象。您必须使用guidata保存它,以便稍后在另一个回调中使用它。
function SerialBtn_Callback(hObject, eventdata, handles)
% hObject handle to SerialBtn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA
comPort = get(handles.COMportTxt,'String');
if(~exist('serialFlag','var'))
[handles.s, handles.serialFlag] = setupSerial(comPort);
end
guidata(hObject,handles)
希望这有帮助。