loaing data from a sequence of files, why it failed?

조회 수: 1 (최근 30일)
Tmat
Tmat 2021년 8월 7일
편집: Stephen23 2021년 8월 8일
Hi guys, I'm trying to load data from mat files named "test1.mat",...,"test100.mat". In file "testii.mat", vector Tn has positive values from row (ii-1)*10+1 to row ii*10 and is filled with zeros for any rows before row (ii-1)*10+1. The length of Tn is 10*ii. I wanna store all these positive values of Tn from the 100 mat files in a single vector named allTn. I used the following loop:
clear;
narray=100;
for ii=1:narray
is=num2str(ii);
name=strcat('test',is);
load(name);
allTn(((ii-1)*10+1:ii*10)',1)=Tn(((ii-1)*10+1:ii*10)',1);
end
However, I got error message "Index in position 1 exceeds array bounds (must not exceed 10)" in the line
allTn(((ii-1)*10+1:ii*10)',1)=Tn(((ii-1)*10+1:ii*10)',1);
I also found that the problem is caused by the fact that the loop index is already equal to 3, but name still equals to 'test1'. This causes the problem as I will be citing elements in Tn that do not exist.
I'm wondering why do I got this message, and how should I fix it? In particular, why name is not updated during the loop?
Thanks!
  댓글 수: 7
Tmat
Tmat 2021년 8월 8일
@Walter Roberson Thank you very much! I found my problem, that is there is another variable in my test1.mat file named ii, which equals 3. After fixing this, my code could run.
Stephen23
Stephen23 2021년 8월 8일
편집: Stephen23 2021년 8월 8일
@Tmat: that is one of the reasons why it is strongly recommended to load into an output (a scalar structure):
S = load(..);
Benefits for you: much more robust code, less wasted time.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by