Code Folding: How to Collaps like "Fold Initially"

조회 수: 2 (최근 30일)
Andre Zeug
Andre Zeug 2021년 9월 30일
댓글: Andre Zeug 2021년 10월 2일
Dear All,
when having large scripts it is a good option to fold some code like functions and sections but not all, which can be set in the Editors Properties called "Fold Initially".
To recover this state (for all m-files) so far it was required to
1. close Matlab,
2. delete the file "%AppData%\MathWorks\MATLAB\R20XX\MATLAB_Editor_State.xml"
3. restart Matlab
With R2021b this Workaround causes some unexpected behaviour. The code is folded correctly, but depicted as unfolded:
Thus it cannot be unfolded (easily). When Collapsed it should be a "+".
Question: How to recover the Code Folding like it is set in Editors Property "Fold Initially" for a specific m-file? (But not "Collaps All")
Is there any Workaround better than deleting "MATLAB_Editor_State.xml"?
Regards Andre
  댓글 수: 1
Andre Zeug
Andre Zeug 2021년 10월 2일
digged a bit further but do not know howto execute "Collapse All" programatically
Otherwise this code could do what I want
function CollapseCurrentScriptLikeFoldInitially()
%% temporary set CodeFolding Preferences like "Fold Initially" Selection
s = settings;
% get Code Folding Preferences
CF = s.matlab.editor.codefolding;
% rearrange Preferences
fn = fieldnames(CF);
fn = sort(fn);
indInit = contains(fn,'Initially');
fnInit = fn(indInit); % "Fold Initially"
fn = fn(~indInit) ; % "Enabled"
TF = {'o','x'}; % TrueFalse for display
for n = 1:length(fn)
fni = fn{n};
myInd = contains(fnInit,fni); % finds the corresponding "Fold Initially"
if any(myInd)
fprintf('%30s: %s %s\n', fni, TF{CF.(fni).ActiveValue+1}, TF{CF.(fnInit{myInd}).ActiveValue+1});
CF.(fni).TemporaryValue = CF.(fnInit{myInd}).ActiveValue;
else
fprintf('%30s: %s\n', fni, TF{CF.(fni).ActiveValue+1});
end
end
%% call KeyStroke "Collapse All"
warning(sprintf('This section does not work!\nNeeded to be replaced by a "CollapseAll" command. :-('))
import java.awt.Robot;
import java.awt.event.*;
RoboKey = Robot;
RoboKey.keyPress(KeyEvent.VK_CONTROL);
RoboKey.keyPress(KeyEvent.VK_COMMA);
RoboKey.keyRelease(KeyEvent.VK_CONTROL);
RoboKey.keyRelease(KeyEvent.VK_COMMA);
% https://stackoverflow.com/questions/25994261/how-to-execute-collapse-all-folds-in-the-matlab-editor-programatically
%% reset code CodeFolding Preferences
for n = 1:length(fn)
fni = fn{n};
if hasTemporaryValue(CF.(fni))
clearTemporaryValue(CF.(fni))
end
end
end

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by