Assigned variables from app designer into base workspace, but they disappear when I run the .m file
13 次查看(过去 30 天)
显示 更早的评论
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.
- Is there a way around this?
- 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 个评论
Stephen23
2019-1-23
编辑:Stephen23
2019-1-24
"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):
回答(1 个)
Guillaume
2019-1-23
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!