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 个评论
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