# Adding a second for loop to create different matrices

21 views (last 30 days)

Show older comments

Inti Vanmechelen
on 22 Dec 2015

Edited: Mohammad Abouali
on 22 Dec 2015

I have this for loop (see below), and I want to create 10 empty 6x3 matrices (S1 to S10). Is there a way to add a second loop that loops over 1 to 10 so I don't have to repeat everything 10 times?

names2 = {'soleus' 'tibant' 'gaslat' 'vaslat' 'rectfem' 'hamlat'};

Vars2 = {soleus, tibant, gaslat, vaslat, rectfem, hamlat};

nvars = length(Vars2);

for s = 1:1:nvars

S1.(names2{s}) = zeros(100,3);

S2.(names2{s}) = zeros(100,3);

S3.(names2{s}) = zeros(100,3);

S4.(names2{s}) = zeros(100,3);

S5.(names2{s}) = zeros(100,3);

S6.(names2{s}) = zeros(100,3);

S7.(names2{s}) = zeros(100,3);

S8.(names2{s}) = zeros(100,3);

S9.(names2{s}) = zeros(100,3);

S10.(names2{s}) = zeros(100,3);

end

##### 1 Comment

Stephen23
on 22 Dec 2015

### Accepted Answer

Mohammad Abouali
on 22 Dec 2015

Edited: Mohammad Abouali
on 22 Dec 2015

Yes, there is a way; but (really really) not recommended: (why not, refer to Stephens answer) But if you insist, here is how you can do it (but again you shouldn't do it)

for s = 1:1:nvars

for idx=1:10

eval(sprintf('S%d.(name2{s}) = zeros(100,3);',idx))

end

end

You better off using array of structure

for s = 1:1:nvars

for idx=1:10

S(idx).(names2{s}) = zeros(100,3);

end

end

##### 2 Comments

Mohammad Abouali
on 22 Dec 2015

Edited: Mohammad Abouali
on 22 Dec 2015

You are correct. That wouldn't work because:

subject(idx) = zeros(6,3);

will treat subject as a regular matrix therefore, subject(id) should have only one element while zeros(6,3) is a matrix.

subject=cell(10,1);

for idx = 1:10

subject{idx} = zeros(6,3);

end

Depending on what you want to do, you might not need cell or structure at all. Depending what your calculations are may be this would work too:

subject = zeros(6,3,10);

This is a three-dimensional array with third dimension being your 10 matrices. The data structure that you use is related to what type of calculation you want to do.

### More Answers (1)

Stephen23
on 22 Dec 2015

Edited: Stephen23
on 22 Dec 2015

Solution

S(1).(names2{s}) = zeros(100,3);

S(2).(names2{s}) = zeros(100,3);

S(3).(names2{s}) = zeros(100,3);

... etc

Obviously you can loop over the indices of the structure. Or you can learn how to write efficient MATLAB code without loops: this allocates all those zeros-arrays to all fields of a 1x10 structure, in just three lines:

>> names2 = {'soleus','tibant','gaslat','vaslat','rectfem','hamlat'};

>> X(1,:) = names2;

>> X(2,:) = {zeros(100,3)};

>> S = repmat(struct(X{:}),1,10);

Why waste your time writing loops when you can do that instead?

And whatever you do, do not define those variable names S1, S2, etc, dynamically.

Why Dynamic Variable Names are a BAD IDEA

Avoid creating dynamically named variables in MATLAB. This is poor practice as has been explained many times on this forum, and is not recommended by MATLAB themselves:

When you are a beginner it seems like a cunning and fast way to store information, but actually it is really bad practice to name your variables dynamically. MATLAB is also not intended for this kind of variable naming: if you continue to include data in the variable names then you will find yourself fighting many more of these battles against MATLAB.

However when you use more appropriate storage for your data (and meta-data) then you will suddenly find lots of MATLAB functions that do many useful operations for you, quickly and easily.

In your case a much more robust solution would be to use:

- the dimensions of numeric arrays: fully vectorized code is often the fastest MATLAB data processing!
- cell arrays
- structures, where you can include fields for each kind of data (e.g. Process type, Flow data, Temperature data, Notes, Units, etc). Structures can even be non-scalar, which is extremely useful! And yes, you can even define structure fieldnames dynamically.

There are many functions that support working on structures and cell arrays and can access their data easily, and many functions operate on complete numeric arrays all at once without any loops (i.e. vectorized code, which is something you need to learn about).

Placing your data in a structure or cell array also makes it much easier to pass to functions: can you imagine the fight you would have trying to pass hundreds of dynamically named variables to a function?

If you have a newer version of matlab you can also use a table , which stores the data together in one array but also allows key-name access to the columns. This might be a good alternative for your data.

In case you are interested, here are some pages explaining why dynamically assigning variable names is a really bad idea in MATLAB:

Here is a discussion of why it is a bad idea to include meta-data (such as an index) in a variable name:

And here is a discussion of why it is a really bad idea to make variables magically appear any workspace (even though beginners love doing this):

And in case you thought this is a MATLAB restriction, here is the same discussion for some other languages, advising "DO NOT create dynamic variable names":

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!