Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Is it possible to control code folding with code?

Asked by per isakson on 14 Feb 2012

There is a public MATLAB Editor API. However, I cannot find any way to manipulate folding of m-source-code with code. I search something similar to the Editor Debug API (dbclear, dbcont, dbdown, dbquit, dbstack, dbstatus, dbstep, dbstop, dbtype).

On my wishlist is

  • store and restore of "the current folding state"
  • set folding line_number on/off
  • get folding line_number
  • and more

Currently, I may display the source code nicely with the help of folding. However, a search will unfold every block in which the string is found and the display is turned into a mess.

Thus, is there a backdoor to the folding functionalities?

0 Comments

per isakson

Products

1 Answer

Answer by Oleg Komarov on 14 Feb 2012

I have not found a smarter way, but you could adopt the following solution, give that test.m contains:

for ii = 1:10
    s(ii) = 20;
end
for ii = 1:10
s(ii) = 20;
end

Open in the editor a give focus to previous document

current = matlab.desktop.editor.getActive;
edtObj  = matlab.desktop.editor.openDocument('C:\Users\ok1011\Desktop\test.m');
current.makeActive

Then smart indent everything and compare what got indented.

% Get unindented doc
txt         = matlab.desktop.editor.textToLines(edtObj.Text);
% Indent
edtObj.smartIndentContents;
txtIndent   = matlab.desktop.editor.textToLines(edtObj.Text);
% Line by Line comparison, true = unchanged, false = got indented
strcmp(txt, txtIndent)

The next step is to make a collage of the unindented code with the indented one, overwrite and save.

0 Comments

Oleg Komarov

Contact us