Info

この質問は閉じられています。 編集または回答するには再度開いてください。

index exceed matrix dimesion error, earlier GUI was running all fine, all sudden it comes like this

2 ビュー (過去 30 日間)
Bibek Shrestha
Bibek Shrestha 2015 年 6 月 18 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
files=files(3:end,:);
j=1;
for i = 1:size(files,1)
filename = files(i,:);
if exist(['SavedCartridgeSettings\',filename]) ==7
% pattern1 = '.mat';
% pattern2 = '\s{2,}';
% replacement = '';
% filename=regexprep(filename,pattern1,replacement);
% filename=regexprep(filename,pattern2,replacement);
x=strsplit(filename,'-');
mpid(j)=x(1);
name=char(x(2)); % index exceeds matrix dimemsion come here
name=strsplit(name,' ');
mfname(j) =name(1);
mlname(j) =name(2);
j=j+1;
end
end
  1 件のコメント
Guillaume
Guillaume 2015 年 6 月 18 日
編集済み: Guillaume 2015 年 6 月 18 日
I would have thought that files would be a cell array, in which case the line
if exists(['...' , filename]) == 7
would throw an error (as filename would be a cell array which can't be concatenated with a string).
Also, the proper way to convert a cell containing a string into a string is by indexing the cell, not with char:
name = x{2}; %instead of name = char(x(2))

回答 (1 件)

Guillaume
Guillaume 2015 年 6 月 18 日
Well, you never check that your filename actually contains a '-'. If it does not, then the cell array x will only have one element, so x(2) is not valid.
Most likely, your files comes form a dir command. When dealing with external input (list of files in a folder, user input from a gui), it is always a good idea to check that this input conforms to what you expect. There's no guarantee that the files in the directory all have a '-' in them (maybe somebody mistakenly put another file in there) or that the user only entered numbers, etc.

この質問は閉じられています。

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by