How to import workspace in a function

11 ビュー (過去 30 日間)
Lila Lotus
Lila Lotus 2020 年 8 月 28 日
編集済み: Lila Lotus 2020 年 8 月 29 日
Hello,
I have written the simple function, which generates and starts a matlab m file (script which must work on base workspace ).
Unfortunately this started file can't access workspace variables. (if I start it directly without script it works ok). how can I solve this? If I convert the function to a script, then how would I pass file name to it? Scripts seems to be without arguments, rigth?
I start it in the directory where it is situated:
generate_and_start('MyFile.gen')
The batch call generates MyFile.m
Now I start this m file and get a error:
Undefined variable "out" or class "out.get".
If I start MyFile.m directly everything works perfect.
Thank you
function res = gebnerate_and_start(script)
clear Functions;
r=dos(['generate.bat ' script]);
if r==0
a=regexp(script,'\.','split');
s=strcat(a{1},'.m');
run(s);
else
disp('error:generation failed')
res=r;
end
end

回答 (1 件)

Jeff Miller
Jeff Miller 2020 年 8 月 29 日
I don't really understand what you are trying to do, but maybe an answer to this question will help:
% If I convert the function to a script, then how would I pass file name to it?
A script has access to the variables in the workspace, so you can
myFileName = 'MyFile.gen'; % set the file name
script_generate_start; % this calls the script, and the script can refer to myFileName
  1 件のコメント
Lila Lotus
Lila Lotus 2020 年 8 月 29 日
編集済み: Lila Lotus 2020 年 8 月 29 日
Thank you for your answer, I am doing somethink like this now, but would prefer some more elegant solution, if available.
I would like to call it like:
generate_and_start ('filename.scr")

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeNumber games についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by