Function executing a Script - Intrested in Scripts Output

3 views (last 30 days)
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 Comment
Jan
Jan on 20 Mar 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.

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 20 Mar 2018
See Jan's comments above, but for your problem, use this:
evalin('base','Script1')

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!