Assigned variables from app designer into base workspace, but they disappear when I run the .m file

9 views (last 30 days)
Hello - I'm new to app designer and created a simple GUI tool. I've made several drop downs that look like this:
% Value changed function: TypeofTestCycleDropDown
function TypeofTestCycleDropDownValueChanged(app, event)
value = string(app.TypeofTestCycleDropDown.Value);
assignin ('base','test_type',value);
end
Pretty simple stuff, and when I use the drop down I see the "test_type" change in my base workspace. I then have a run button to run a .m file. The file doesn't clear the workspace or anything, but as soon as I hit "Run.m" in app designer my base workspace is now cleared.
  1. Is there a way around this?
  2. Also for the drop down, I noticed the value only changes if I change the drop down selection. For example to have the variable populate the first drop down selection, I have to click to another selection, then back to the first one. Is there an elegant way around this? Right now I just made a blank selection the first thing in the drop down but I don't care for it much.
Thanks !
  4 Comments
MustangManiac
MustangManiac on 23 Jan 2019
Ok so the variables I assign to base appear in "Workspace", but when I do "Run.m" the name changes to "Workspace - Run". Which might explain it? I thought base was someplace everything could have access to but maybe I'm not correct.
Stephen23
Stephen23 on 23 Jan 2019
Edited: Stephen23 on 24 Jan 2019
"I thought base was someplace everything could have access to but maybe I'm not correct. "
Nope, that is not correct. Not only that, but making variables magically appear in other workspaces is a slow, complex, obfuscated way to write code. Read this to know some of the reasons why:
" I did check and it literally clears the workspace as soon as the m file is run (the first line of code). All that still exists is "app" and "event" in the workspace. "
No, your function does not "clear" any workspace. You were looking at the function workspace, which contains only the variables defined in that function (e.g. the input arguments that you describe). Every function has its own independent workspace (and independent is exactly how they should be):
You should learn to pass variables using simpler and more efficient methods, e.g. as function input and output arguments (which is exactly what the MATLAB documentation recommends):

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 23 Jan 2019
Most likely, the code in Run.m is a function not a scripts. Probably a function that has optional inputs (or no inputs at all) if it runs without errors.
Functions always have their own workspace (which disappear when the function ends). The proper way to get variables into the function workspace is by passing these variables as inputs to the function. And the proper way to get variables out of the function workspace is with the function outputs.
In general assignin and co. should be avoided.

Community Treasure Hunt

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

Start Hunting!