Index exceeds Matrix dimensions

Hello
I am trying to run the following block of code-
for q = patient_num(1):patient_num(end)
flag=[];
i=0;
num = num2str(patient_num(q));
for j= file_num(1):file_num(end)
file = num2str(file_num(j));
record_file = strcat('explained','_','ch','_',num,'_',file,'-percent-explain-10', '.mat');
cd ('/scratch/group/eegnw/EEG/DATA/')
flag[i]=load('-mat',record_file);
i=i+1;
end
end
I want to check if this is running correctly or not by looking at the value of i at the end. I give inputs as patient_num=1:3, file_num=2:4. However, after running the outer loop for one patient_num, I get an error saying Index exceeds matrix dimensions. Basically the value of patient_num has changed from a vector [1 2 3] to just a scalar 1. I can't figure out why that is happening as my code doesn't modify that anywhere. Any help is appreciated.
Expected output- i=9

回答 (3 件)

Walter Roberson
Walter Roberson 2016 年 7 月 7 日

1 投票

Using that form of load() is not recommended. It is recommended that instead you assign the output of load() to a variable. The result will be a structure with one field for each variable stored in the file. You then extract the needed parts of the structure. This approach cannot accidentally overwrite unexpected variables like your current approach can.

1 件のコメント

Mridul Garg
Mridul Garg 2016 年 7 月 7 日
I tried that, introducing a variable names flag=[], and storing the output of load in it. Still the same error

サインインしてコメントする。

James Tursa
James Tursa 2016 年 7 月 7 日

0 投票

Is there a variable named patient_num in the mat file?

6 件のコメント

Mridul Garg
Mridul Garg 2016 年 7 月 7 日
I'm defining patient_num and file_num beforehand as 1:3 and 2:5
James Tursa
James Tursa 2016 年 7 月 7 日
Yes, but you stated that patient_num had changed. Loading a mat file could change it if the mat file had a variable named patient_num.
Mridul Garg
Mridul Garg 2016 年 7 月 7 日
Oh, okay. I think that might be the issue, let me try changing the variable name.
Mridul Garg
Mridul Garg 2016 年 7 月 7 日
No, changing that doesn't help.
James Tursa
James Tursa 2016 年 7 月 7 日
編集済み: James Tursa 2016 年 7 月 7 日
Please show your current code. Are you sure that patient_num is the expected vector at the start of this code? What happens if you comment out the load command and then run this section of code?
Mridul Garg
Mridul Garg 2016 年 7 月 8 日
I updated the code but it gives me another error now-
Error: Unbalanced or unexpected parenthesis or bracket.

サインインしてコメントする。

Star Strider
Star Strider 2016 年 7 月 8 日

0 投票

The square bracket here is not legal MATLAB syntax:
flag[i]=load('-mat',record_file);
consider instead:
flag{i} = load('-mat',record_file);

4 件のコメント

Mridul Garg
Mridul Garg 2016 年 7 月 8 日
If I do that, I get the previous error-
Index exceeds matrix dimensions.
Star Strider
Star Strider 2016 年 7 月 8 日
I can’t run your exact code because I don’t have your files or other data.
Run this version to see where the problem might be:
patient_num = 1:3; % Create Data
file_num = 1:3; % Create Data
for q = patient_num(1):patient_num(end)
flag={};
i=1;
num = num2str(patient_num(q));
for j= file_num(1):file_num(end)
file = num2str(file_num(j));
record_file = strcat('explained','_','ch','_',num,'_',file,'-percent-explain-10', '.mat');
% cd ('/scratch/group/eegnw/EEG/DATA/')
% flag[i]=load('-mat',record_file);
flag{i} = record_file;
i=i+1;
end
end
Note that the counter is reset inside the first loop. You may want to intitalise it before the ‘q’ loop. (I changed the initialisation to 1 because MATLAB does not allow zero or negative or non-integer indices, and you increment it after the ‘flag’ assignment.)
Walter Roberson
Walter Roberson 2016 年 7 月 8 日
Star Strider, is it part of the debugging that you do not load() the file?
Star Strider
Star Strider 2016 年 7 月 8 日
Here, yes. The indexing seems to be the problem, so sorting that first may be appropriate.
Also, there are no files to load. I would create one or more, but I have no idea what they actually contain, so I can’t simulate what the code does. I don’t understand where the matrix dimensions problem arises (other than initialising ‘i’ in the inner loop and expecting it to eventually equal the sum of iterations of the inner and outer loops, since it will currently count only to whatever the inner loop limit is).

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2016 年 7 月 7 日

コメント済み:

2016 年 7 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by