najusten
najusten

Reputation: 59

Only add existing variables in Matlab

I would like to add the sums of 5 one-column arrays in Matlab. The catch is that depending on previous inputs, any of these arrays may or may not exist, thus throwing an error when I try to add the sums of these arrays for post-processing.

After doing some digging I found the following function which I can use to return a logical statement if a certain variable exists in the workspace:

exist('my_variable','var') == 1

I'm not sure this helps me in this case though - Currently the line in which I add the sums of the various arrays looks as follows:

tot_deviation = sum(var1) + sum(var2) + sum(var3) + sum(var4) + sum(var4);

Is there a short way to add only the sums of the existing arrays without excessive loops?

Any help would be appreciated!

Upvotes: 2

Views: 301

Answers (3)

LNiederha
LNiederha

Reputation: 948

To my knowledge there is no quick way to do this in matlab. It seems to me that, depending on the structure of your code, you have the following alternatives:

1- Initialize all your variables to column arrays of zeros with something like var = zeros(nbLines, 1);

2- Put all your columns vectors side to side in a single array and the use tot_deviation = sum(sum(MyArray)); which will work no matter how many columns and lines there is in the array.

3- If you pass your variables to a function you can check the number of inputs arguments inside the function using 'nargin' and then only proceed to sum the right number of variables.

I would recommend using the second method for it seem to me that it is the one that allows you to take the most advantage of matlab's array system which good.

Upvotes: 3

Wolfie
Wolfie

Reputation: 30047

The most robust solution is to initialise all of your variables to 0 at the top of the function. Then there is no chance they don't exist, and they influence the summation correctly.


Alternatively...

You could (read: shouldn't) use a really nasty eval trick here for flexibility...

vars = {'var1','var2','var3','var4'};
tot = 0;
for ii = 1:numel(vars)
    if exist(vars{ii}, 'var')
        tot = tot + eval(var);
    end
end

I say it's "nasty" because eval should be avoided (read the linked blog). The check on the variable name existence mitigates some of the strife, but it's still not ideal.

As suggested in the MathWorks blog on evading eval, a better option would be a struct with dynamic field names. You could use almost the same syntax as above, but replace the if statement with

if isfield( myStruct, vars{ii} )
    tot = tot + myStruct.(vars{ii});
end

This will avoid dynamically named variables and keep your workspace clean!

Upvotes: 2

Rotem
Rotem

Reputation: 32084

You can use if statements:

if ~exist('var1','var'), var1 = 0;end
if ~exist('var2','var'), var2 = 0;end
if ~exist('var3','var'), var3 = 0;end
if ~exist('var4','var'), var4 = 0;end

tot_deviation = sum(var1) + sum(var2) + sum(var3) + sum(var4) + sum(var4);

Upvotes: 4

Related Questions