Function executing a Script - Intrested in Scripts Output

3 次查看(过去 30 天)
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 个评论
Jan
Jan 2018-3-20
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.

请先登录,再进行评论。

采纳的回答

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

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by