Gui problem with workspace

5 views (last 30 days)
AStro
AStro on 5 Oct 2021
Edited: Stephen23 on 5 Oct 2021
Hi,
I have created gui (using Guide) but I faced a problem. My gui has 2 buttons at the moment - one for opening the file and reading in variables, second for making a plot. Each of the buttons reffers to separate .m file. Opening button runs .m file with the script that opens the file and reads the data to the workspace, whereas the second button (that is activated after the first button was used) refers to another .m file with plots (grapf and nmerical data in the table). The opening button seems to works, but by pressing second button I get error message. I looks like it does not see the variables that should be ploted. I know that gui is using different workspace than 'base' workspace, but I thought that untill I am within the same gui I am also within the same workspace so that the varialbes that should be used for ploting are easly accessible. Apparenly it is not the case (?). My question is, how to address to the workspace which stores the demanded variables?
Unrecognized function or variable 'freq'.
Error in CWT_ResSpec (line 8)
plot(freq,ampl,'b','LineWidth',0.5)
Error in run (line 91)
evalin('caller', strcat(script, ';'));
Error in CWT_software_gui>ResSpec_Callback (line 88)
run('CWT_ResSpec.m')
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in CWT_software_gui (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)CWT_software_gui('ResSpec_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
  3 Comments
AStro
AStro on 5 Oct 2021
Thank you for the links. Unfortunately it is not straightforward to me on how to execute the sharing data. I am using Guide to create the gui and since it is very new to me I am not certain what commands should I use and where in gui (or .m file) should I locate them.
From what I noticed it has somtething to do with
btn.ButtonPushedFcn = {@...,txt,pnl};
Rik
Rik on 5 Oct 2021
Use the guidata struct. If you're using GUIDE, this will be retrieved every time a callback function is called.
Just make sure to store the data you want to be available for other calls as fields of that struct and use guidata(hObject,handles) to save your new data in that struct.

Sign in to comment.

Answers (1)

Rik
Rik on 5 Oct 2021
Why do you want to use evalin? Just have your functions return outputs.
Every function has its own workspace. To share data between different GUI elements you can use the guidata struct (or properties in case of AppDesigner).
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.

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!