Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Adding Arrays Together
Replies: 1   Last Post: Mar 6, 2013 9:48 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View  
Steven Lord

Posts: 17,944
Registered: 12/7/04
Re: Adding Arrays Together
Posted: Mar 6, 2013 9:48 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply



"S Stron" <ss708@york.ac.uk> wrote in message
news:kh6vdc$kll$1@newscl01ah.mathworks.com...
> Yeah the only problem is they aren't my variables, I've just been sort of
> assigned to the analysis of the task!
> I know it'd be a million times easier with a different type of array but I
> was wondering if it was at all possible without changing anything?


Yes, but storing your variables like that is going to bite you in the butt
repeatedly in terms of code clarity and performance (since you're likely
going to need to litter your code with EVAL statements) and distinguishing
which variables are in the workspace to be summed and which are not could be
error-prone. Suppose that you had a different 3-by-4 array named DoNOTAdd in
your workspace -- writing a flexible and general procedure that can
determine that you need to add A, B, C, D, E, etc. without adding DoNOTAdd
could be tough.

If you _can't_ avoid this naming scheme, you can at least mitigate it a bit
in your code. Don't LOAD the data into separate variables; LOAD it into
fields of a struct array and use dynamic field names.

data = load(myMatFile);
listOfFields = fieldnames(data);
data.(listOfFields{1}) % Extract the contents of one of the variables from
the MAT-file

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.