How to run and analyze a big Matlab class code line by line.

조회 수: 5 (최근 30일)
Arti Siddhpura
Arti Siddhpura 2014년 5월 23일
답변: James Kristoff 2014년 5월 24일
I am new to Matlab classes and I am working on a big Matlab class (600+ lines in total) where I need to check outcome from each executable line in terms of variables being used and returned and their size, type etc. so that I can remove unnecessary variables and preallocate all necessary variables. I tried putting break points but it seems that Matlab goes upto end of methods and not beyond that where actual work starts. I am aware of mlint, tic-toc and profiler for optimizing Matlab codes but for that I need to run the whole thing first. Can anyone please let me know if it is posssible at all to run a Matlab class with breakpoints?
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2014년 5월 24일
Yes, it is possible to put breakpoints in a class definition and have the code break in the appropriate place. I often do this.
Note that if you ever change the code, then you must run clear all to refresh the class definition. Maybe that is why you are having problems - changes are being made to the class definition, a new instance of that class is created using the old definition and stepping through the code is confusing.
Cedric
Cedric 2014년 5월 24일
편집: Cedric 2014년 5월 24일
Note that e.g.
clear all
clears all break points. Therefore, if you have a script in which you create an object based on a classdef which contains break points, if the script starts will clear all, your break points are gone. So keep in mind that you may have to comment out a few calls to clear before you run the script.

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

채택된 답변

James Kristoff
James Kristoff 2014년 5월 24일
You can place a breakpoint at any point that you would like to ensure you stop at. I am not sure what you mean about work starting after the end of the function. In a MATLAB class, all 'work' happens inside of the class's methods. If, for example you have the following class:
classdef foo < handle
properties
bar
end
methods
% class constructor
function obj = foo(obj)
obj.bar = 'hello';
% do some work
obj.doWork();
end
function doWork(obj)
disp(obj.bar);
end
end
end
and you then want to see how this class works you can put a breakpoint on the line:
obj.bar = 'hello';
and the execute the command:
aFooObject = foo;
in MATLAB®. This should stop at the breakpoint, and then you can step through the code using the F10 key (in Windows). When you get to the line:
obj.doWork();
you can step into this function using the F11 key (in Windows), which will allow you to debug the doWork() function as well.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by