Index exceeds matrix dimensions for fprinf

6 ビュー (過去 30 日間)
FLGATOR
FLGATOR 2018 年 2 月 19 日
回答済み: FLGATOR 2018 年 2 月 27 日
Hi there,
I kept getting this message below. I would appreciate it if you could help me out.
Here is the the code I ran below
formatSpec = '%s\t %.15f\t %.15f\t %.15f\n';
fid = fopen('sineBurstAnalysis.txt','w');
fprintf (fid, '%s\t %s\t %s\t %s\n',...
'FileName','CVburstDuration','CVburstTP','CVburstAMP');
for r = 1:8
fprintf(fid,formatSpec,ddd(1,r).name, cvDuration_1(1,r), cvTTP_1(1,r), cvAMP_1(1,r));
end
fclose(fid);
Here is the error message I got below.
Index exceeds matrix dimensions.
Error in sineBurstAnalysis_test1 (line 160)
fprintf(fid,formatSpec,cvDuration_1(1,r), cvDuration_1(1,r), cvTTP_1(1,r),
cvAMP_1(1,r));
Thank you for your help!

採用された回答

Jan
Jan 2018 年 2 月 19 日
編集済み: Jan 2018 年 2 月 19 日
Use the debugger to check the cause of the error: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html . Set a breakpoint in the failing line, or even better let Matlab stop automatically at the error:
dbstop if error
Now run the code again until it stops. The error message is clear: one of the used variables does not have as many elements as requested:
size(ddd)
size(cvDuration_1)
size(cvTTP_1)
size(cvAMP_1)
The forum cannot suggest a way to fix the problem, because we cannot know, how these variables have been created. But the debugger will help you here also.

その他の回答 (2 件)

Star Strider
Star Strider 2018 年 2 月 19 日
If your data are column vectors rather than row vectors, you will get that error:
x = rand(5,1); % Column Vector
q = x(1,3);
Index exceeds matrix dimensions.
Error in ... (line ###)
q = x(1,3);
The solution for that is to only use one index to refer to elements of a vector, rather than two.

FLGATOR
FLGATOR 2018 年 2 月 27 日
I resolved the issue. I appreciate all of your help!

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by