GUI doesn't run M-file (error) while manually running (F5) this M-file doesn't give any problems.
Show older comments
I have made some quite complex m scripts which work fine when I run them next to each other. Now I want to make it easier to run (without switching between m-files) by using a GUI.
I have made some buttons which run a different m-file each.
- First I run a 'reset' script with a 'reset' button which runs a starting plot and defines some variables and matrices/arrays. With the assignin function I managed to put this variables in the Workspace so that I can use them for the other scripts. This works fine.
- Then I can include some variables in the GUI textbox which are included in the workspace as well, again with the assignin function. This works fine too.
- But then, with another button I want to run another m-file (which needs variables and a plot defined before). Then I get a lot of errors. However, when I run this m-file manually with F5, everthing works fine. What do I do wrong?
These are the errors I got (the sys variable is one of the variables I define in the GUI, and this is succesfully included in the workspace):
Undefined function or variable 'sys'.
Error in Holecheck (line 51)
if sys==1
Error in Interfacetest>pushbutton_add_Callback (line 165)
Holecheck
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Interfacetest (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Interfacetest('pushbutton_add_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Error using assignin
Too many output arguments.
Error in Interfacetest>popupmenu_system_Callback (line 135)
sys=assignin('base','sys',(get(hObject,'Value')))
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Interfacetest (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Interfacetest('popupmenu_system_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Error using evalin
Argument must contain a string.
Error in Interfacetest>popupmenu_system_Callback (line 135)
sys=evalin('base',(get(hObject,'Value')))
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Interfacetest (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Interfacetest('popupmenu_system_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Answers (1)
Walter Roberson
on 24 Jul 2017
You have
sys=assignin('base','sys',(get(hObject,'Value')))
However, assignin() does not return a value.
You have
sys=evalin('base',(get(hObject,'Value')))
however, get() of a uicontrol popup is always numeric, so you are asking to evalin('base', NUMERIC), which is not permitted syntax.
... I think you should look at
and
8 Comments
Rick van den Hadelkamp
on 24 Jul 2017
Walter Roberson
on 24 Jul 2017
We do not have your code code (or your .fig)
Rick van den Hadelkamp
on 24 Jul 2017
Walter Roberson
on 24 Jul 2017
Your code appears to rely upon sys having been assigned in the base workspace but at the moment I do not see anywhere in the code that you assigned it in the base workspace. You access it from application data at one point but at the moment I do not see it being assigned there
"Your code appears to rely upon sys having been assigned in the base workspace..."
And that is exactly why magically making variables "poof" into existence and jump between workspaces using assignin leads to such buggy (and hard to debug) code. Passing arguments is trivially easy, using nested arrays is simple, and both are much simpler to debug: why not learn some good coding methods today?
Walter Roberson
on 25 Jul 2017
Nested arrays, Stephen?
Stephen23
on 25 Jul 2017
@Walter Roberson: errr... there is a question mark, but I am not sure what the question is. I find nested arrays quite convenient for passing data around a GUI internally (between callbacks). Obviously it won't get data into the base workspace, but that it then pretty trivial using output arguments.
Walter Roberson
on 25 Jul 2017
I could see nested structures being used for that but I would probably use nested functions with shared variables.
Categories
Find more on Plot Customization 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!