From: Steven Lord on

"Stefano " <cillino25(a)yahoo.it> wrote in message
news:hvt7rg$bq6$1(a)fred.mathworks.com...
> Maybe people that's POSTING can't THINK enough well because they're new to
> the subject. I said sorry, there's no need to discuss.
>
> However, I opened this post because I'm new in structs and I'm new in
> Matlab as well:
> i'm discovering it, and it's really powerful.. i thought that what I
> wanted could be done with only one easy istrunction (there's lot of stuff
> much more complicated than this which is resolved in only a couple of
> instructions).

This is true. But one thing that people sometimes complain about with
MATLAB is that there are _too many_ instructions and that it can be hard to
find the right one for the task. So while there _could_ be a built-in
"scale a particular field from each struct in a struct array" command, there
isn't. But it's easy enough to build one -- just make a function of your
own. For example, place the following in a file named scaleField.m
somewhere on your MATLAB path:


function myStruct = scaleField(myStruct, fieldname, scaleFactor)
% SCALEFIELD Scale a field of each struct in an array by a scale factor
%
% Add in your own help text here

% Add error checking as appropriate
for k = 1:numel(myStruct)
myStruct(k).(fieldname) = myStruct(k).(fieldname)*scaleFactor;
end


This makes use of dynamic field names [the .() syntax] so I don't need to
hard-code which field of the struct to scale. Search the documentation for
"dynamic field names" for more information on this syntax.

Now in the rest of your code, the "scale a particular field from each struct
in a struct array" command is one instruction that uses the scaleField
function you wrote:


s = scaleField(s, 'value', 2) % scales each struct's value field by
multiplying by 2


--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com