tags:

views:

82

answers:

3

I need to calculate the mean, std, and other numbers for my programming and I was wondering how to use the loop function to my advantage. I have 5 electrodes of data. So to calculate the mean of each I used:

mean_ch1 = mean(ch1);
mean_ch2 = mean(ch2);
mean_ch3 = mean(ch3);
mean_ch4 = mean(ch4);
mean_ch5 = mean(ch5);

What I want is to be able to condense that code into a line or so. The code I tried does not work:

for i = 1:5
mean_ch(i) = mean(ch(i));
end

I know this code is wrong but it conveys the idea of what I'm trying to accomplish. I want to end up with 5 separate variables that are named by the loop or a cell array with all 5 variables within it allowing for easy recall. I know there must be a way to write this code I'm just not sure how to accomplish it.

+4  A: 

You have a few options for how you can do this:

  • You can put all your channel data into one large matrix first, then compute the mean of the rows or columns using the function MEAN. For example, if each chX variable is an N-by-1 array, you can do the following:

    chArray = [ch1 ch2 ch3 ch4 ch5];  %# Make an N-by-5 matrix
    meanArray = mean(chArray);        %# Take the mean of each column
    
  • You can put all your channel data into a cell array first, then compute the mean of each cell using the function CELLFUN:

    meanArray = cellfun(@mean,{ch1,ch2,ch3,ch4,ch5});
    

    This would work even if each chX array is a different length from one another.

  • You can use EVAL to generate the separate variables for each channel mean:

    for iChannel = 1:5
      varName = ['ch' int2str(iChannel)];  %# Create the name string
      eval(['mean_' varName ' = mean(' varName ');']);
    end
    
gnovice
If you have sets of data like this, arrays/vectors are *the* way to go in Matlab. Using vector operations will result in significantly faster code (in most cases) over using loops/iterators.
bta
Thank you for your response it has helped me greatly reduce my coding. The first one did not separate the array as I would have liked but did work. The second one did not work for my MATLAB, @mean seems to cause a problem. The EVAL worked perfectly as I wished, thank you very much.
A: 

If it's always exactly 5 channels, you can do

ch = {ch1, ch2, ch3, ch4, ch5}
for j = 1:5
    mean_ch(j) = mean(ch{j});
end

A more complicated way would be

for j = 1:nchannels
    mean_ch(j) = eval(['mean(ch' num2str(j) ')']);
end
Marc
A: 

Apart from gnovice's answer. You could use structures and dynamic field names to accomplish your task. First I assume that your channel data variables are all in the format ch* and are the only variables in your MATLAB workspace. The you could do something like the following

%# Move the channel data into a structure with fields ch1, ch2, ....
%# This could be done by saving and reloading the workspace
save('channelData.mat','ch*');
chanData = load('channelData.mat');

%# Next you can then loop through the structure calculating the mean for each channel
flds = fieldnames(chanData); %# get the fieldnames stored in the structure

for i=1:length(flds)
     mean_ch(i) = mean(chanData.(flds{i});
end
Azim