A PB GUI enters the callback routine, upon completin of the callback routine, it jumps to the gui_mainfcn.m..I expected it to return to the GUI without calling the gui_mainfcn.m

5 views (last 30 days)
I'm running a small gui code, started from the GUIDE command, selected blank, then added a PB, when the PB is selected, execute code:
% --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
[filename, pathname, filterindex] = uigetfile('*.*', 'Select blackbox data file in XL format'); filelocation=strcat(pathname,filename);
[num0,txt0]=xlsread(filelocation,1); %buffer 0
This works fine, it opens a file I select, it does the xlsread as shown.....when it finishes that line of code, it then jumps/calls the gui_maninfcn.m which then wipes out the workspace and the variables that were just created....
It seems I am not correctly ending the pb callback code which then causes it to run the gui_mainfcn.m code.....
need help to understand this novice error...
thanks gary

Accepted Answer

Geoff Hayes
Geoff Hayes on 6 Oct 2014
Gary - the behaviour you describe, .....when it finishes that line of code, it then jumps/calls the gui_maninfcn.m which then wipes out the workspace and the variables that were just created...., is expected. Neat that you were using the debugger to step through the code and observe this! Once the callback has executed, any local variables that you had created within that function are lost/cleared as you move out of scope. This is no different than local variables created/declared within functions from most other languages (i.e. C/C++).
So you want to keep the data that you have read in to the variables num0, txt0, and perhaps even the file that you read in to filelocation. Do you want to access this data from within another callback of your GUI? If so, then you can save this data to the handles structure which is the third input to your pushbutton1_Callback, so that other callbacks can make use of it.
For example, you can update your callback as follows
function pushbutton1_Callback(hObject, eventdata, handles)
[filename, pathname, filterindex] = uigetfile('*.*', ...
'Select blackbox data file in XL format');
filelocation=strcat(pathname,filename);
[num0,txt0]=xlsread(filelocation,1); %buffer 0
% save the data to the handles structure
handles.filelocation = filelocation;
handles.num0 = num0;
handles.txt0 = txt0;
guidata(hObject,handles);
Once you have set the fields of the structure with the new data, then you must call guidata to store that data. Now, in any other callback that receives this structure as an input (and if it doesn't, then you can use guidata to obtain the handles structure), you can access the data that you saved to it. For example, if you have another push button then
function pushbutton2_Callback(hObject, eventdata, handles)
% write out the name of the file you loaded from push button 1
if isfield(handles,'filelocation')
fprintf('file loaded: %s\n',handles.filelocation);
else
fprintf('file has not yet been loaded!\n');
end
Note that the handles structure also contains all handles to the widgets that you have added to your GUI.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!