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 个评论
Daan Decleer
Daan Decleer 2017-4-24
It's not in a function. I have the variable "OPTIONS.Analysis" = 1 in my base workspace. The second .m file has the line: "if (OPTIONS.Analysis==1) ..."
But here the notification comes: Error using Interface2 (line 9) Undefined variable "OPTIONS" or class "OPTIONS.Analysis".
And the it is in the workspace, I cheked it.
Stephen23
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
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.

Walter Roberson
Walter Roberson 2017-4-24
OPTIONS = evalin('base', 'OPTIONS');

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by