The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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: 18,038
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" <> wrote in message
> 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
To contact Technical Support use the Contact Us link on

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

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.