1

I have a 2x2 design research and I have over 150 participants. So I have 4 different conditions and I named them as 11, 12, 21, 22.

I need to transform all my cell values to matrices with cell2mat and need to do that for each participant. I am trying to write a for loop to have different values for each participant, but I could not figure out how to do it.

 %% each participant records

%conditions are = 11 , 12 , 21, 22
%participants 1:101 (p1,p2,p3.....p150)

for i = 1:150
%personal control (pc)
p_pc11= cell2mat(A.pc11(i,:));
p_pc12= cell2mat(A.pc12(i,:));
p_pc21= cell2mat(A.pc21(i,:));
p_pc22= cell2mat(A.pc22(i,:));

 save('p?_pc11' , p1_pc11)

end

It should be something like this but I do not know how to save each variable differently. I tried p(i)_pc11 , p'i'_pc11, but it gave errors. I need to have a unique variable for each participant. I am keeping them in structs but it is not good for running anova or manova.

So my question is, how can I save each participants value with different names?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • Do you want to save the various variables *inside* MATLAB (bad, very bad), or *outside* MATLAB (easily done)? – Adriaan Jan 25 '18 at 22:19
  • why do you want to create a variable for each participant, and not just contain everything in a matrix. (row # is participant #)... – bla Jan 25 '18 at 22:25
  • @Adriaan why saving inside the matlab is very bad? – australopithecus Jan 25 '18 at 22:27
  • @bla If I do that will it let me run manova with demogrpahics for each participant? Also I have lots of variables like pc such as offers, responses, reaction times etc... – australopithecus Jan 25 '18 at 22:30
  • You can just create an array of 150 zeros and fill it. For example, for the first one, before the loop you write `p_pc11 = zeros(150,1)`, then the loop: `for i = 1:150 p_pc11(i)= cell2mat(A.pc11(i,:));` – rvbarreto Jan 25 '18 at 22:34
  • 2
    @rvbarreto DON'T do that please. `zeros(a)` will create a square matrix of dimensions a-by-a, which is probably not what the OP wants. *Always* use two numbers within `zeros`, `ones`, `nan` etc, like `zeros(150,1)`, to make sure it ends up being the dimension you actually want it to be, and not something square and much larger than anticipated. – Adriaan Jan 25 '18 at 22:35
  • @Adriaan. Nice. zeros(150,1) then :) – rvbarreto Jan 25 '18 at 22:37

1 Answers1

2
for ii = 1:150
%personal control (pc)
p_pc11= cell2mat(A.pc11(ii,:));
p_pc12= cell2mat(A.pc12(ii,:));
p_pc21= cell2mat(A.pc21(ii,:));
p_pc22= cell2mat(A.pc22(ii,:));

% Generate numbered filenames
filename11 = sprintf('p_pc11_%d',ii);
filename12 = sprintf('p_pc12_%d',ii);
filename21 = sprintf('p_pc21_%d',ii);
filename22 = sprintf('p_pc22_%d',ii);

% Save them with the numbered name
save(filename11 , p_pc11)
save(filename12 , p_pc12)
save(filename21 , p_pc21)
save(filename22 , p_pc22)

end

Use sprintf to generate numbered filenames, then save those. Note that I changed your loop index from i to ii, as i is the imaginary unit.

Note that using numbered variable names, or indeed any kind of dynamically named variable, inside of MATLAB is very, very bad for performance and readability, and thus debugging, issues, see this answer of mine and the references to MathWorks sources contained therein for a more in depth explanation.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • thanks for your answer and recommendation! I was going to run some statistical analysis after I save each variable differently but it seems like not a good idea to do – australopithecus Jan 25 '18 at 22:39
  • I am storing all those variables in a struct, when i need to access them I can retrieve them by typing `A.pc11(1,:)` but I cannot run anova with them. @Adriaan – australopithecus Jan 25 '18 at 22:44
  • @australopithecus I have no idea what anova is or how it works, so I can't help you there. If it's inside MATLAB: chuck in inside a `for` loop and get each structfield to a temporary variable, and thus out of the struct, before plugging it into anova. Don't try to save all 150 variable dynamically, that'll just mess up everything in your script. – Adriaan Jan 25 '18 at 22:46
  • I am going to try it! Thank you very much! I am a novice and a learner for matlab but now i can see a solution for my question thanks to your answer :) – australopithecus Jan 25 '18 at 22:50