MATLAB: How to change a number in a variable name inside a for loop in order to acces to other similar variables

for loopnum2str

Hello,
I have 480 vectors that I want to compare their means and STD, in pairs so I want 240 comparisons.
the variable names that I want to compare are:
marker1x vs lmarker1x; marker1y vs lmarker1y; marker1z vs lmarker1z; . . . marker48x vs lmarker48x; marker48y vs lmarker48y; marker48z vs lmarker48z;
I have created a loop to do this:
for i=1:48
p_value_var(i)=vartest2(['marker',num2str(i),'x'],['lmarker',num2str(i),'x'])
p_value_mean(i)=ttest2(['marker',num2str(i),'x'],['lmarker',num2str(i),'x'])
end
But in this way matlab treats ['marker',num2str(i),'x'] as a string,
Is there any other function to do this wich matlab treats the oputput as a variable name?

Best Answer

First, you need to rescue your vectors from their existence as dynamically-named variables. This unfortunately requires that you use the eval function, but you only have to use it once for each vector:
markerx = cell(48,1); % Preallocate
lmarkerx = cell(48,1);
markery = cell(48,1);
lmarkery = cell(48,1);
markerz = cell(48,1);
lmarkerz = cell(48,1);
for k1 = 1:48
markerx{k1} = eval(['marker',num2str(k1),'x']);
lmarkerx{k1} = eval(['lmarker',num2str(k1),'x']);
markery{k1} = eval(['marker',num2str(k1),'y']);
lmarkery{k1} = eval(['lmarker',num2str(k1),'y']);
markerz{k1} = eval(['marker',num2str(k1),'z']);
lmarkerz{k1} = eval(['lmarker',num2str(k1),'z']);
end
Then do your statistical tests on the cell arrays.
Related Question