Scalar assignment and nargout bump
Show older comments
I am trying to do the following:
s = [objs.s] % s may be a struct array (really object array) or empty []
[s.prop] = assignToStructureArray(scalar_value);
where
function varargout = assignToStructureArray(value)
varargout(1:nargout) = {value};
end
This works fine unless s is empty, in which case nargout is 1, not 0 and suddenly s is no longer empty.
This brought me to Loren's Blog post which greatly confuses me. http://blogs.mathworks.com/loren/2009/04/14/convenient-nargout-behavior/
I'm really having trouble understanding why the nargout bump is a good idea. It seems to me like this is just another Matlab corner case that while sometimes convenient is not a good idea.
Any help on understanding the post and more importantly, on how to change the code to work without having to resort to an if statement before making the function call that checks whether s is empty or not would be great.
Thanks, Jim
3 Comments
Jim Hokanson
on 14 Aug 2013
Edited: Jim Hokanson
on 15 Aug 2013
My goal is to write a function which removes the initial setup of performing deal. Those steps are replication and then transfering data to a cell format so that they can be passed in as multiple inputs to deal.
I'm not sure when this comment occurred in the flow of our conversation, but hopefully you're now aware that it is not necessary to replicate scalars with deal(). The single line of code
[s(1:n).prop]=deal(scalar)
will distribute the scalar n times exactly as you would hope.
Accepted Answer
More Answers (0)
Categories
Find more on Whos 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!