loaing data from a sequence of files, why it failed?
2 ビュー (過去 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 で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!