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

2 views (last 30 days)
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!

Accepted Answer

Walter Roberson
Walter Roberson on 7 May 2013
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);

More Answers (1)

Iman Ansari
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

Community Treasure Hunt

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

Start Hunting!