MATLAB Answers

Jennifer

How to assign a column vector a name in a matrix while processing multiple files (loop)?

Asked by Jennifer
on 7 May 2013

Hi, We have 100 .mat- data files containing 5 column vectors. We would like to calculate the average of the 4th column for each of those 100 .mat- data files. Here is a sample of one of our .mat-files:

    0.6797    0.2551    0.2543    0.8308    0.0540
    0.6551    0.5060    0.8143    0.5853    0.5308
    0.1626    0.6991    0.2435    0.5497    0.7792
    0.1190    0.8909    0.9293    0.9172    0.9340
    0.4984    0.9593    0.3500    0.2858    0.1299
    0.9597    0.5472    0.1966    0.7572    0.5688
    0.3404    0.1386    0.2511    0.7537    0.4694
    0.5853    0.1493    0.6160    0.3804    0.0119
    0.2238    0.2575    0.4733    0.5678    0.3371

We tried the following code but we were not able to assign the 4th column the name “HR1” to calculate its mean for each person/.mat- data file.

%% Set path
cd ('N:\backups\Jennifer T.\HR data analysis')
thePath.data = fullfile(pwd,'subject data','PTSDdata','to be added');
thePath.programs = fullfile(pwd,'programs');
addpath(genpath(thePath.data))
% Select multiple Out4 files and open them
cd (thePath.data)               
[F,P]=uigetfile('*.*','MultiSelect', 'on'); 
Re=zeros(length(F),1);
for i = 1:length(F)
    HR1=??(:,4);
    Re(i)= nanmean(HR1)
End

Any suggestions on how to do this? Thank you!

  0 Comments

Products

No products are associated with this question.

2 Answers

Answer by Walter Roberson
on 7 May 2013
 Accepted answer

http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

Also, it is recommended to use the functional form of load:

data = load(F{i});
varnames = fieldnames(data);
firstvar = varnames{1};
HR1 = data.(firstvar)(:,4);

  2 Comments

Jennifer
on 8 May 2013

Thank you, this was very helpful for solving our problem!


Answer by Iman Ansari
on 7 May 2013

Hi. What is the variable name that you want to calculate its 4th column average? if the name of the variable is the same in all files:

 for i = 1:length(F)
    load(F{i})
    HR1=Variable_name(:,4);
    Re(i)= nanmean(HR1);
 end

  0 Comments


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today