direct access to the result of a function call
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuesta aceptada
James Tursa
el 24 de Feb. de 2012
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)
Más respuestas (1)
Walter Roberson
el 24 de Feb. de 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.
Ver también
Categorías
Más información sobre Whos en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!