Any new possibilities on dynamic code folding possibilities for vs 2021a?
1 view (last 30 days)
Show older comments
Hi All,
I was wondering if there have been any new capabilities added to Matlab in the recent versions to allow users to dynamically set code folding/unfolding within the Matlab Editor? This has been a question asked in the past (~2016-2017, e.g. Is it possible to control code folding with code? - MATLAB Answers - MATLAB Central (mathworks.com)) but haven't seen any inquires on the subject recently. I have looked at the Matlab Editor API to see if any new updates have been added to allow this functionality but it does not appear to me to have happened.
My goal is to take large scripts that have not been functionalized and allow user to specify at beginning of script which sections to unfold by using structure field names. This would greatly increase the efficiency of debugging these large scripts.
For example, if you would like to keep all code folded except section where a structure field "testField" is created, than can specify at beginning of script:
unfoldCodeSectionFieldList = {'testField'};
Then when that field is first initialized within the script, unfold the code that follows where the values assigned within the sub-fields of "testField" are defined within the script. As a simple way of illustrating what I am trying to say, an example might be:
unfoldCurrSectionFlag = false;
struct.testField = {};
if isfield(struct,'testField') && isempty(struct.testField)
unfoldCurrSectionFlag = true;
end
if unfoldCurrSectionFlag
%% Unfold here
struct.testField.data1 = [1 1 1 1];
struct.testField.data2 = [2 2 2 2];
end
unfoldCurrSectionFlag = false;
ideally I could functionalize this to run on all scripts, aka:
function unfoldScriptTest(struct,unfoldCodeSectionFieldList,scriptName)
if isfield(struct,'testField') && isempty(struct.testField)
% use Matlab Editor API to create code section ("%% Dynamically added code section") in "scriptName" at place of "testField" initialization
end
end
I would like to be able to do this without having to use a publishing workaround(such as asked about in this post:
and which references:
I understand there are likely easier ways to write the above code, I am simply using it as an easy way to explain, so please - rather than correcting the code above - please let me know if there is a better way to explain what I am after with the main subject of this inquiry.
I do realize that the more efficient way in the long-term would be just to functionalize the script and better organize it overall, however, as a workaround for very long scripts where I do not have the time to do so - this type of functionality would be extremely helpful.
I appreciate any responses!
Thank you,
Quentin
2 Comments
Rik
on 17 Aug 2021
In my experience, debugging long scripts takes longer than you might expect. I would urge you to convert the code to functions. It will speed up debugging and give you folding options for free. Functions have they enormous benefit that they are a black box that you can debug without context after which you don't need to worry about its internal workings.
Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!