My Matlab.m file can't find variabels in the base workspace. How can I solve this.
2 次查看(过去 30 天)
显示 更早的评论
Hello
I've wrote a program as following: -I declare a lot of variabels in an matlab.m file. -Once they are all declared an interface.mlapp file (an application created in app designer) opens and with an nice interface I can change the variabels. -If i click 'OK' in the interface an seconde matlab.m files opens and does calculations with the variabels in the workspace.
The problem is that my seconde matlab.m files doesn't find variabels WHICH ARE in the base workspace. Do I have to move all my variabels into an function workspace? and how do I do that? It are a lot of variabels...
I look forward to your answers.. Daan Decleer
3 个评论
Stephen23
2017-4-25
编辑:Stephen23
2017-4-25
"My Matlab.m file can't find variabels in the base workspace. How can I solve this."
There is nothing to solve because a function workspace should be separate from the base workspace. That is the whole point of them! Think about it: do you know what happens inside of sin? Do you know what internal variables it uses? Would you want the output of sin to change depending on what was in the base workspace? This is the point of functions: their workspaces are independent. And this is how they should be.
Instead: convert all of your scripts into functions. Do NOT put work variables into the base workspace. Scripts are great for playing around with some code, but if you want to do reliable code and reproducible code outputs then you need to use functions.
Move data between workspaces reliably and simply by passing them as arguments (perhaps in struct), exactly as the MATLAB documentation recommends:
Do not use globals, assignin, or evalin or any other slow, buggy hack code.
回答(2 个)
Image Analyst
2017-4-25
I agree with Adam. Make the first m-file script into a function, and put the values into a struct and pass the struct out to your second m-file which will call your first m-file. Passing variables between different workspaces is kludgy and complicated (using things like setappdata, evalin, assignin, etc.) whereas this is precisely what functions are made for.
Further info is in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!