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

조회 수: 3 (최근 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