Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Matlab include all the dimensions of the expression when error is thrown because dimensions not agree?

Tags:

matlab

So I am debugging some Matlab code and I get the dimension-doesn't-agree-error for some expressions. It's all nice that Matlab points to the correct line etc. However it would be nice if Matlab would output the dimensions of the variables involved in the error text so I don't have to deal with sizing them up myself. Sometimes for a long expression deep in a for loop it's a real hassle to figure out what exactly all dimensions are.

So is there a setting or hack for this?

like image 394
Reed Richards Avatar asked Jan 22 '23 19:01

Reed Richards


2 Answers

The easiest way to deal with this issue is to type dbstop if error in the command window, and then to run the code. MATLAB will then stop execution right before it would throw the error, and it will open the editor on the line where the error would be thrown. Then you can inspect the array sizes at your leisure, and you can even, in the command window, try out possible fixes, because you will have access to all the variables currently active in the code.

like image 74
Jonas Avatar answered Mar 21 '23 10:03

Jonas


You can try the try-catch-end block.

E.g.

try
    %# Some error prone code
    a = getA(b);
catch err_msg
    %# Display any information you want 
    disp(size(b));
    %# Display the error message
    disp(err_msg.identifier);
    disp(err_msg.message);
end 

You can also throw in a breakpoint in the catch block if you want to evaluate things yourself.

like image 20
Jacob Avatar answered Mar 21 '23 11:03

Jacob