How do you assign into a function space a variable that exists in the caller script?

I have a an anonymous function that uses specific inputs. When I call the function from the caller script, I want to also pass a parameter vector into the function space. However, I can't seem to do this correctly.
I am trying this repeatedly using the "assigning" function, with various permutations of 'base' and 'caller," and can't seem to make it work.

4 件のコメント

Stephen23
Stephen23 2018 年 1 月 17 日
It would be simpler and more efficient to parameterize the function, rather than relying on slow and buggy assignin:
Or using nested functions:
Patrick Anderson
Patrick Anderson 2018 年 1 月 17 日
Thanks, Stephen and Walter.
I tried both of these approached previously. For some reason--or maybe multiple reasons--they did not work with the functions I was using. The Matlab documentation for these is confusing, and in some cases, ambiguous. I will try again with your advice and let you know if it works.
Steven Lord
Steven Lord 2018 年 1 月 17 日
If you try Stephen's and Walter's advice and it still doesn't work, I recommend showing a small segment of code to illustrate concretely what you're trying to do. Your original problem description was very general, so the advice we could give also could only be general. The more detailed and specific the description of the problem we have, the more focused our suggestions can be.
Patrick Anderson
Patrick Anderson 2018 年 1 月 17 日
編集済み: Patrick Anderson 2018 年 1 月 17 日
The "nested" suggestion turned out to be the best one, but was a little difficult to figure out from the Matlab documentation. Thank you, Stephen, for the suggestion.
The "assignin" documentation is downright ambiguous and, in my opinion, should be extensive improved.

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

 採用された回答

Walter Roberson
Walter Roberson 2018 年 1 月 17 日
Demonstration code:
function at
junk = 123;
freedo = 222;
foo = @(junk) fprintf('%d, %d\n', alter1()+junk, alter2()+junk)*0+freedo;
foo(junk)
function r = alter1
assignin('caller', 'junk', 987);
assignin('caller', 'freedo', 111);
r = 0;
function r = alter2
assignin('caller', 'junk', 456);
assignin('caller', 'freedo', 888);
r = 0;

1 件のコメント

Patrick Anderson
Patrick Anderson 2018 年 1 月 17 日
We ended up using the "nested" suggestion. However, I appreciate this suggestion as well. I recommended the Mathworks improve their description of this commmand also.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by