New to GUIDE in MATLAB, Frustrated by error...

1 view (last 30 days)
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

Accepted Answer

Geoff Hayes
Geoff Hayes on 11 Oct 2014
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.
  1 Comment
Noel
Noel on 11 Oct 2014
Thanks, Geoff. I think the reason the first line didn't give the error is DiffFile is a static text block in my GUI, but DiffFilePath is not. I'll try setting the handles.DiffFilePath like you suggest.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!