Gui problem with workspace

5 次查看(过去 30 天)
AStro
AStro 2021-10-5
编辑: Stephen23 2021-10-5
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 个评论
AStro
AStro 2021-10-5
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 2021-10-5
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.

请先登录,再进行评论。

回答(1 个)

Rik
Rik 2021-10-5
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.

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by