2

基本的に定数と文字列の定義を含むスクリプトを呼び出すネストされた関数があります。これらの変数をベース ワークスペースに渡す必要があります。私はそれらをglobal、最善の解決策ではないはずだと定義できることを知っていますよね?

関数の出力引数を使用する従来の方法は、私の場合は複雑すぎるようです。(実際には1回限りの呼び出しなので、コードを爆破したくありません)assigninandを使用することを考えwhoましたが、セル配列でもコンマ区切りのリストでも機能しないようです。おそらく、いくつかの構文の改良が欠けているだけです。

function myFunction()

myScriptWithDefinitions;

% who returns a cell array with all variables from my script
temp = who;
% now I try to assign these variables to my base workspace
% these are my attempts, none of them working
assignin('base',who);
assignin('base',temp{:});
assignin('base',{temp{:}});

...
end

名前のリストと値のリストの両方を実際に渡す必要があることは承知しています。さらにアイデアはありますか?


編集:次のようなもの

assignin('base',{'A','B'},{2,5})
% or 
assignin('base',{'A',2},{'B',5})

機能しないためassignin、一般的にはオプションではないと思います。

4

1 に答える 1

2

with assignin you can only assign-in 1 variable at once.

With "who" you get a cell-array of strings, that contains the names of the variables. Now if you have this list:

myVarList=who;

you can loop over and assign the variables to the workspace:

myVarList=who;

for indVar = 1:length(myVarList)
    assignin('base',myVarList{indVar},eval(myVarList{indVar}))
end

Note: this is an eval-solution... if someone knows a quick replacement for that, please let me know :)

于 2013-10-21T10:30:29.400 に答える