Happy new year guys, I have a simple question please, I am writing a code in a for loop and it still gives me an error saying Index exceeds matrix dimensions. and it refers to a line that doesn't have matrix in it ! so seems like this is not the line with the error, how can i please track exactly where the error is?

 채택된 답변

Walter Roberson
Walter Roberson 2017년 12월 31일

0 개 추천

At the command line, try giving the command clear with the function name, like
clear azl_assignment_7
This will remove any copies of the code that it has in memory. This can be needed if you modify code outside of the MATLAB editor, or if you write code files in the program and want to execute the code, or if you had taken the handle of the function in the past.
The only other time I have seen it give the wrong line number was cases where the code had a syntax error, in which case sometimes it gets the wrong line in reporting about the syntax error -- but in such cases the code will not execute at all.

댓글 수: 9

MAHMOUD ALZIOUD
MAHMOUD ALZIOUD 2017년 12월 31일
I tried this but still gives the same error, my code is like 1400 lines and nested in a for loop followed by plotting code. it does not make sense to give error in a line that has nothing to do with the matrix dimension, when i copied this line outside this script it ran without any problems ! but inside the loop it gives error
Walter Roberson
Walter Roberson 2017년 12월 31일
편집: Walter Roberson 2017년 12월 31일
Can you attach the code and any files you need for us to test with (you can zip the files to be able to attach them)
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD 2017년 12월 31일
i attached the code and the 2 sample cards to be read, thank you sir
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD 2017년 12월 31일
in the code i am analyzing 7 classes of vehicles (from class 4 to class 10), everything works well except for class 10 !! knowing that i did it the same way for the other classes
You have
%case1: the first 4 are quad and then single aingle:
x1_10= M10{K}(:,8)*0.1*3.28084>1 & M10{K}(:,8)*0.1*3.28084<6 & M10{K}(:,10)*0.1*3.28084>1 & M10{K}(:,10)*0.1*3.28084<6 & M10{K}(:,12)*0.1*3.28084 >1 & M10{K}(:,12)*0.1*3.28084 <6 & M10{K}(:,14)*0.1*3.28084 >6 & M10{K}(:,16)*0.1*3.28084 >6;
This attempts to access M10{K}(:,16) but none of your data has more than 15 columns (and is constructed to have only 15 columns.)
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD 2017년 12월 31일
Ok you make sense here, but the strange thing is why it works when I take the class 10 code to a separate script and run it? it gives no error there, but when i nest it in the FULL code it gives error Mr Walter
Then M{K} must have had at least 16 columns in the separate script. For example in what you posted in https://www.mathworks.com/matlabcentral/answers/366134-how-can-i-please-reduce-the-code-running-time you break out more than 15 columns.
All of your cases for M10 try to use column 16, except for case23 and case24. Unfortunately you failed to comment clearly as to what columns mean what. You should probably have used a table() object for your purposes, or separate arrays.
Checking further I see you tried to use column 17 as well.
I have attached a version of your code that is cleaned up a bit.
After the clean-up changes, I suspect that column 16 is intended to be E F Axle Spacing, and that column 7 is intended to be F Axle Weight. I think you will agree that the changed version is much easier to read in that regards.
Amazing as usual Mr Walter, thank you very much for your wonderful help. you made sense in many points you mentioned and I will take them into consideration, thank you for the clean up code. and happy new year for you and your blessed family.
I just tried it and it worked well, Thank you again very much for pointing out the error source, you were right, I added 2 more columns to class 10.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by