direct access to the result of a function call

1 回表示 (過去 30 日間)
grapevine
grapevine 2012 年 2 月 24 日
Hi,
Is there any way to write this code
" portdata=get_param(gcb,'PortConnectivity')
In1Src=portdata(1).SrcBlock
" in this way
" In1Src=get_param(gcb,'PortConnectivity') (1).SrcBlock
" ?
I need to do that 'cause my prof (advisor of my internship) told me to do that, he doesn't want too many variables in the workspace, because in his opinion they can interfere with the proper functioning of the other blocks, that means I must avoid to create portdata
thank a lot 4 reading let me know if you have any idea

採用された回答

James Tursa
James Tursa 2012 年 2 月 24 日
This question in various forms has been asked multiple times on this forum and on the newsgroup. The answer is no, you cannot do the one-step approach in MATLAB. You can create some obfuscated code that looks like your one-liner, but it will essentially have the two-liner executing in the background and will not be more efficient than the two-liner. If you don't like having portdata around after extracting the part you want then simply delete it. If your professor is forcing you to use some type of one-liner approach then he/she needs to be educated about how MATLAB works. I would be interested in hearing how the presence of a variable interferes with the proper functioning of the other blocks. (Side Note: MATLAB may be enabling this type of syntax in a future version, so my no answer is not the final word)
  1 件のコメント
grapevine
grapevine 2012 年 2 月 24 日
so I guess no grafting code my prof ll be sad to learn that and me more released
by the way thanks a lot :)

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 2 月 24 日
Here is the code you need in order to avoid creating a named temporary variable:
In1Src = subsref( subsref(get_param(gcb,'PortConnectivity'), struct('type',{'()'},'subs',{{1}})), struct('type',{'.'},'subs',{'SrcBlock'}) );
Reading that is "cruel and unusual punishment" in my opinion.
Your existing code of
portdata = get_param(gcb,'PortConnectivity');
In1Src = portdata(1).SrcBlock;
differs from the above only in that it creates an explicit name for a temporary data structure that goes unnamed in the longer and difficult to read code. If you do not want the temporary variable to be kept, use your existing code but add
clear portdata
I do not recommend explicit use of subsref() when it is avoidable. I only show it here in order to demonstrate that it is possible but that it is the programming equivalent of a tension headache.
  1 件のコメント
grapevine
grapevine 2012 年 2 月 27 日
GREAT
thank yyou so much

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by