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

조회 수: 2 (최근 30일)
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!

채택된 답변

Walter Roberson
Walter Roberson 2013년 5월 7일
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);

추가 답변 (1개)

Iman Ansari
Iman Ansari 2013년 5월 7일
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

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by