在MATLAB GUI中更新回调状态

时间:2013-10-17 03:01:26

标签: matlab user-interface matlab-guide

我在MATLAB中开发了一个GUI界面。当我按下按钮搜索时,我看到了理想的结果。但是,当我更改文本框并再次按下搜索按钮时,它不起作用并且给出了以下错误:

Undefined function 'untitled2' for input arguments of type 'struct'.

Error in @(hObject,eventdata)untitled2('edit1_Callback',hObject,eventdata,guidata(hObject))


Error while evaluating uicontrol Callback

Undefined function 'untitled2' for input arguments of type 'struct'.

Error in @(hObject,eventdata)untitled2('pushbutton16_Callback',hObject,eventdata,guidata(hObject))


Error while evaluating uicontrol Callback

我必须重新执行所有代码!有没有办法重复运行GUI?

如图所示,当我将视频ID更改为其他号码并按下搜索按钮时,结果不会更新。

function pushbutton16_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton16 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%pathname='C:\Users\Dr Syed Abdul Rahman\Desktop\innovation final\video detail\';
string1 = get(handles.edit1,'UserData');
fName=strcat(cd,'\Video Detail\Video Detail',string1);
fid = fopen(fName);
if fid~=-1
 s{1} = fgetl(fid);
 s{2} = fgetl(fid);
 s{3} = fgetl(fid);
 s{4} = fgetl(fid);
 s{5} = fgetl(fid);
 s{6} = fgetl(fid);
 s{7} = fgetl(fid);

set(handles.text4,'Visible','On');
set(handles.edit1,'Visible','On','String',s{1})
set(handles.edit2,'Visible','On','String',s{2})
set(handles.edit3,'Visible','On','String',s{3})
set(handles.edit4,'Visible','On','String',s{4})
set(handles.edit5,'Visible','On','String',s{5})
set(handles.edit6,'Visible','On','String',s{6})
set(handles.edit7,'Visible','On','String',s{7})
set(handles.axes4,'Visible','On');
cd './Images';
A = imread(s{1});
axes(handles.axes4)
imshow(A);

else
 set(handles.text3,'Visible','On','String','File is not exist !') 
end

3 个答案:

答案 0 :(得分:2)

而不是这一行“string1 = get(handles.edit1,'UserData');”

试试这个 string1 = get(handles.edit1,'String');

答案 1 :(得分:1)

pushbutton16_Callback中有很多奇怪的事情发生了:

  • 您无需为所有编辑框设置'Visible','on'
  • get 'String'就像amir nemat所说,而不是'UserData'
  • 使用fullfile代替strcat
  • 不要忘记fclose(fid)
  • 除非你cd './Images'回来,否则不要在回调中cd,但即便如此,这也不是一个好主意,只需imread进入该路径。
  • 执行imshow(A,'Parent',handles.axes4)而不是axes(handles.axes4); imshow(A);

此外,您可能希望将GUI重命名为untitled2以外的其他内容。 ;)

至于你收到错误的原因,我不确定,但我怀疑当gui_mainfcn尝试feval你的untitled2.m运行回调时,它正在运行别的。检查其他untitled2 MATLAB可执行文件:which -all untitled2

答案 2 :(得分:0)

使用时更改工作文件夹可能会出现问题:

cd './Images';

可能的纠正可能是:

oldPath = cd('./Images'); % Return the path that you were before
A = imread(s{1});
axes(handles.axes4)
imshow(A);

cd(oldPath);  % Go back in the folder with all your functions