필터 지우기
필터 지우기

Pass Parameters to Script

조회 수: 114 (최근 30일)
Andrew Barton
Andrew Barton 2012년 10월 24일
댓글: Steven Lord 2018년 11월 30일
Hello All,
I wanted to know if anyone knows if it's possible to pass arguments to a matlab script file, perhaps similar to the built-in commands like clear.
My motivation for wanting to do this instead of using a function file is that in my case, I would strongly prefer all variables used to remain in the workspace so that I can continue playing around with them. One option would be to put everything into a struct and have the function output that struct, but this is very cumbersome and muddles up the script. Thus far I've been putting flags at the beginning of the script (such as "save = true" or "filename = 'FN'") and commenting and uncommenting them as necessary, but as their number has grown and I switch more frequently it would be great to be able to avoid having to open and edit the file so frequently.
Many thanks! Andrew
  댓글 수: 1
Matt J
Matt J 2012년 10월 24일
What exactly do you mean by "pass arguments to a script file" anyway? Since scripts share the workspace of their caller, all parameters in that workspace are already available to the script.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2012년 10월 24일
This FEX submission makes it very easy to export variables to the base workspace
W = who;
putvar(W{:})

추가 답변 (2개)

Matt J
Matt J 2012년 10월 24일
편집: Matt J 2012년 10월 24일
My recommendation would be to use a function file, not a script, but to insert a KEYBOARD statement as the last line in the file. Then when the function completes, you will stay inside the workspace of the function and can continue to play around interactively with the variables there.
This is even a bit safer than what you propose, because every time you call the function, you will always start with a clear workspace. With scripts, it's very easy to forget about the effects on the workspace of previous runs.

Andrew Barton
Andrew Barton 2012년 10월 24일
편집: Andrew Barton 2012년 10월 24일
Well, I found a solution, which is just to use a function at the end call assignin on each variable in the workspace. I put this snippet at the end and it works fine:
varnames = who;
for i = 1:length(varnames)
assignin('caller', varnames{i}, eval(varnames{i}));
end
Since it pains me in my NEVER USE EVAL bone I think I'll keep the question open though. It would be nice to know if there was some sort of native functionality to pass arguments to scripts that I simply haven't found.
  댓글 수: 3
Victor
Victor 2018년 11월 30일
This is really helpfull solution to turn any function into script and any script into function.
Also, may be helpful for debug.
Steven Lord
Steven Lord 2018년 11월 30일
Rather than using assignin and eval to do your debugging, use the debugging tools included in MATLAB to debug your functions.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by