loaing data from a sequence of files, why it failed?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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.
More reasons for avoiding loading directly into the workspace: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!