New to GUIDE in MATLAB, Frustrated by error...
1 次查看(过去 30 天)
显示 更早的评论
I am writing my first real GUI in Matlab using GUIDE. I am getting an error message and I believe my syntax is correct. My button press callback is:
% --- Executes on button press in ChooseDiffusionFile.
function ChooseDiffusionFile_Callback(hObject, eventdata, handles)
[DiffFile,DiffFilePath,~]=uigetfile('*.csv');
display(DiffFile)
display(DiffFilePath)
set(handles.DiffFile,'string',DiffFile);
set(handles.DiffFilePath,'string',DiffFilePath);
% hObject handle to ChooseDiffusionFile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
The error message is:
Reference to non-existent field 'DiffFilePath'.
Error in PlotDataGUI>ChooseDiffusionFile_Callback (line 94)
set(handles.DiffFilePath ,'string', DiffFilePath);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in PlotDataGUI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)PlotDataGUI('ChooseDiffusionFile_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Line 94 is the line: set(handles.DiffFilePath,'string',DiffFilePath);
I am displaying the variables DiffFile and DiffFilePath so I know they have the correct values. This is frustrating because it looks like everything is correct, but I am still getting the error. Can you help me?
Noel Montgomery
0 个评论
采纳的回答
Geoff Hayes
2014-10-11
Noel - the error message is telling you that your code is trying to reference non-existent fields in the handles structure. So the line
set(handles.DiffFilePath ,'string', DiffFilePath);
will fail since DiffFilePath has not been set as a field within the handles structure. (I'm kind of surprised that the previous line didn't generate the same error.)
When setting fields within the handles, the typical way to go about this is as follows
% set the fields
handles.DiffFile = DiffFile;
handles.DiffFilePath = DiffFilePath;
% save the data
guidata(hObject,handles);
Since handles, within your callback, is a copy of the structure that contains all handles to widgets/controls and user-defined data, we must call guidata to save the updated handles structure. Upon doing so, these new (or updated) fields will be accessible in other callbacks that access the handles structure.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!