Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
Tmat
Tmat am 7 Aug. 2021
Bearbeitet: Stephen23 am 8 Aug. 2021
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 Kommentare
Tmat
Tmat am 8 Aug. 2021
@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 am 8 Aug. 2021
Bearbeitet: Stephen23 am 8 Aug. 2021
@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.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by