views:

50

answers:

2

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?

+2  A: 

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.

Jacob
Good idea sometimes you forget that Matlab is integrated with Java and that you could easily apply stuff from there over here.
Reed Richards
@Reed: True. I generally throw in a breakpoint in the `catch` block so I can inspect things manually if I'm there.
Jacob
+6  A: 

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.

Jonas
Way to go! Just made my life 100% simpler.
Reed Richards

related questions