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
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
2018 年 3 月 20 日
See Jan's comments above, but for your problem, use this:
evalin('base','Script1')
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!