My Matlab.m file can't find variabels in the base workspace. How can I solve this.

2 visualizaciones (últimos 30 días)
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 comentarios
Daan Decleer
Daan Decleer el 24 de Abr. de 2017
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 el 25 de Abr. de 2017
Editada: Stephen23 el 25 de Abr. de 2017
"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.

Iniciar sesión para comentar.

Respuestas (2)

Image Analyst
Image Analyst el 25 de Abr. de 2017
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 el 24 de Abr. de 2017
OPTIONS = evalin('base', 'OPTIONS');

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by