Code Folding: How to Collaps like "Fold Initially"

3 visualizzazioni (ultimi 30 giorni)
Andre Zeug
Andre Zeug il 30 Set 2021
Commentato: Andre Zeug il 2 Ott 2021
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 Commento
Andre Zeug
Andre Zeug il 2 Ott 2021
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

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Performance and Memory in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by