direct access to the result of a function call
Show older comments
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
Accepted Answer
More Answers (1)
Walter Roberson
on 24 Feb 2012
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 Comment
grapevine
on 27 Feb 2012
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!