Function executing a Script - Intrested in Scripts Output

12 Ansichten (letzte 30 Tage)
Soren
Soren am 20 Mär. 2018
Kommentiert: Soren am 20 Mär. 2018
Hello,
I'm trying to combine my work in a simple function, which calls some Scripts. My Problem is that the OutputVariables won't show up in the Workspace (probably because functions use a different workspace than Scripts/CommandWindow). I want to use the function to declare a (global) variable that should be used in all Scripts. In the End I am interested in the Variables set in the Scripts.
My Function looks like:
function testfunction(x)
global d;
d = x;
Script1
end
and the Scripts do all kind of simple things:
%Script1
global d;
A = zeros(2,2);
A(1,1) = 3*d;
Now my Problem is that the Matrix A would not show up (in the WorkSpace) after calling 'testfunction(1)'. But if i worked in the CommandWindow with:
global d;
d = 3;
Script1
It would work as inteded. Obviously i run more Scripts and more complex things inside the Scripts. Reworking all Scripts as functions is the least thing i would like to do. I debuged and it confirmed that everything works well up to the point where the function ends. Then all the Variables I am interested in get cleared out of the workspace.
Thanks in Advance
  1 Kommentar
Jan
Jan am 20 Mär. 2018
Scripts and global variables are two bad ideas to organize your work. As you can see already both is confusing and prone to bugs. Prefer functions and provide the data by inputs and outputs.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 20 Mär. 2018
See Jan's comments above, but for your problem, use this:
evalin('base','Script1')

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by