Need help with looping data files in

Hi, I've been working on writing this script that's supposed to read in 24 files, in sets of 3 as there are 3 trials being ran for different weight amounts. My issue is that it's only keeping the last 3 files worth of data while overwritting the rest. I know its due to how I set up my for loops I'm just unsure of how to fix it, as I've been looking around and trying different things without any luck.
HeaderRowCount = 7;
trials = 3;
for idx = setdiff(0:10:150,[30:10:40 60:10:90 130:10:140])
for R = 1:trials
A = importdata(['Static_' num2str(idx) 'g_5in_T' num2str(R) '.txt'], '\t', HeaderRowCount);
Strain{R} = A.data(1:end-1,2);
M(R) = mean(A.data(1:end-2,2));
end
end
Here's my code for reference. The reason I have that array in the setdiff is due to my weights being 0,10,20,50,100,110,120,150. Any help would be great, thanks.

답변 (1개)

Kevin Holly
Kevin Holly 2022년 1월 20일
편집: Kevin Holly 2022년 1월 20일

0 개 추천

HeaderRowCount = 7;
trials = 3;
idx = setdiff(0:10:150,[30:10:40 60:10:90 130:10:140]);
for i = 1:length(idx)
for R = 1:trials
A = importdata(['Static_' num2str(idx(i)) 'g_5in_T' num2str(R) '.txt'], '\t', HeaderRowCount);
Strain{i,R} = A.data(1:end-1,2);
M(i,R) = mean(A.data(1:end-2,2));
end
end

댓글 수: 2

Alternatively, you could write the index as such:
idx = [0,10,20,50,100,110,120,150]
David Morris
David Morris 2022년 1월 20일
편집: David Morris 2022년 1월 20일
Hi, so I just figured it out. I changed it to this
clc;
clear;
close all;
HeaderRowCount = 7;
trials = 3;
i = 0;
for idx = [0,10,20,50,100,110,120,150]
i = i + 1;
for R = 1:trials
A = importdata(['Static_' num2str(idx) 'g_5in_T' num2str(R) '.txt'], '\t', HeaderRowCount);
Strain{i,R} = A.data(1:end-1,2);
M(i,R) = mean(A.data(1:end-2,2));
end
end
I'm getting an 8x3 maxtrix which is great, thanks for the help.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Scripts에 대해 자세히 알아보기

태그

질문:

2022년 1월 20일

편집:

2022년 1월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by