sum of all the variables in workspace how to find?

I have some 100 numbers in the workspace with big names. I want to find the sum of all these variables in the workspace - without writing the big equation for it. Is there a direct way to find the sum of all the numeric variables in the workspace?

2 Comments

Adam
Adam on 27 Feb 2018
Edited: Adam on 27 Feb 2018
If you want to sum them why are they in individual variables in the first place instead of being in an array, which is the natural place for a bunch of numbers for which taking the sum is an appropriate action?
There may be some horrendous way using 'whos' and 'eval', but I never use eval so I wouldn't know the syntax even if it does exist.
" Is there a direct way to find the sum of all the numeric variables in the workspace?"
No, there is no direct way to achieve this. And the indirect ways are very bad ways to write code: pointlessly complex, slow, buggy, hard to debug.
Learn to use arrays because that is what MATLAB is for. If you want to learn how to use MATLAB efficiently then use arrays.

Sign in to comment.

 Accepted Answer

clear all;
x=10;
y=2;
z=3;
allvariables = whos;
sumOfVars = 0;
for i = 1:length(allvariables)
sumOfVars = sumOfVars + eval(allvariables(i).name);
end

6 Comments

Note that this code is not "direct", as the original question requests: it requires using slow introspective command whos and then slow and buggy command eval to do the heavy lifting, so it is rather indirect.
But really there is no reason to be forced into writing such slow, buggy, complex code like this: if the OP had simply put all of the data into one vector vec then all they would need is this:
sum(vec)
The name MATLAB comes from "MATrix LABoratory", and not from "lets put all of the data into lots of separate variables and make accessing the data complex and slow".
The ironic bit is that because the answer combines two of the most common bad practices, it does not even work for the purpose stated. The code starts by the dreaded clear all, deleting all the variables that the OP wanted to sum in the first place.
@Noam, if Answers allowed downvoting, you can be sure that your answer would have been downvoted to oblivion. Yes, you're solving the immediate problem but you're not solving the underlying problem that is the splitting of the data into multiple variables. You're giving a fish but not teaching how to fish!
It's an example, I know that clear all clears all (really, I'm that good with Matlab). The example shows how it sums up x,y and z. So nothing ironic here.
I do agree that this code is not recommended as a common practice. Only to solve the current problem. In the future, prefer a vector as everybody said.
Q: "I want to find the sum of all these variables in the workspace"
A:
clear all
...
I find that ironic.
Thank you all for the comments and answers. I have learnt some new things. Also from comments - what to avoid, the bad practices. Both the solution and the comments are new learning for me.
"what to avoid, the bad practices"
e.g. putting all of your data into lots of separate variables and then using eval.

Sign in to comment.

More Answers (1)

No there is no direct way to do that. You should avoid the problem by not using separate variables for them to begin with.
Adding all the variables is possible but the cleanest approach is probably to save the workspace to a file and to load the file into a struct and use one of several ways to add the fields to the struct.

Products

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!