Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
workspace variables

Subject: workspace variables

From: Puneet

Date: 22 Aug, 2013 09:14:13

Message: 1 of 3

i have 20 variables in the workspace each having one element. is there any way to sum the elements???

Subject: workspace variables

From: Kevin Hellemans

Date: 22 Aug, 2013 09:41:15

Message: 2 of 3

Try using a combination of the who function (returns ws variable names) and the eval function. The code below works, but you might want to try and replace the loop by a cellfun or something like it.

% Put some variables in the workspace
a = 5;
b = 3;
c = 2;

d = who; % Retrieve workspace variables

% Store variable values in array
for i = 1:numel(d)
    e(i) = eval(d{i});
end
% Get sum of all workspace variables
swv = sum(e);

Regards,
Kevin

Subject: workspace variables

From: Steven_Lord

Date: 22 Aug, 2013 13:35:41

Message: 3 of 3



"Puneet " <puneet.988@gmail.com> wrote in message
news:kv4kp5$hd3$1@newscl01ah.mathworks.com...
> i have 20 variables in the workspace each having one element. is there any
> way to sum the elements???

Are your variables named x1, x2, x3, etc. or something similar?

If so, DON'T DO THAT. See question 1 in the Programming section of the FAQ
for alternatives you can use instead that will make it easier to sum the
elements.

http://matlab.wikia.com/wiki/FAQ

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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us