I am having an issue where the code folding doesn't seem to be following the normal protocols I expected it to. I expect the code folding to condense functions, for/while loops, and multi-lined comments. But I am seeing cases where the folding is happening from seemingly arbitrary lines.
For reference, I believe this code was originally written earlier this year on MATLAB R2021b but I have upgraded to R2023a in the past few weeks. I didn't notice the code folding issue until just now but I am unsure how long it's been present.
I apologize in advance for the large sections of code but from what I have seen elsewhere, code folding issues are often hard to replicate so I am keeping my original syntax in the code snippets.
Example 1:
Original Code: Lines 258 - 284
[~,itemp1] = min(abs(tshift(:,:,2)+0.1));
[~,itemp2] = min(abs(tshift(:,:,2)-0.1));
[iKulRef0(i),res] = findchangepts(dat.Ch2(itemp1(i):itemp2(i),i),...
tKul0(:,i,:) = tKul0(:,i,:)-tshift(itemp1(i)+iKulRef0(i)-1,i,2);
iKulRef0 = itemp1.' + iKulRef0 - 1;
Folded Code: Folds such that Lines 259 and 279 are adjacent and I have the following code visible
tKul0(:,i,:) = tKul0(:,i,:)-tshift(itemp1(i)+iKulRef0(i)-1,i,2);
iKulRef0 = itemp1.' + iKulRef0 - 1;
This code folding goes across a section break and includes both comments and code which makes no sense to me. Also this code folding option doesn't dissappear when I uncomment line 258 which invokes a separate function (in an external script) called AutoSaveResultsByRun. There don't appear to be any code folding issues in that separate function either.
Example 2:
Original Code: within local function part of same script - Lines 460 to 520. The confusion gets way worse as the folding and unfolding in this section is direction dependent because of internal code folds somehow.
if j <= 2; temp = kPa.(['Ch',num2str(j)]);
else; temp = dat.(['Ch',num2str(j)]);
y = temp(ia(j,i):ib(j,i),i);
d4spec.(['Ch',num2str(j)])(:,i) = y;
disp('Truncated Data-for-Spectra Calcs has been created')
function event = findExpWaveArrival(ia,ib,d,plotExpWaveID)
global Cruns; global SavePNG;
temp = d.Ch2(ia(i):ib(i),i);
[ipt,~] = findchangepts(temp,'Statistic','linear');
event(i,2) = ia(i)+ipt-1;
templeg = {sprintf('Run %d: Ref Kul',Cruns.Run_num(i)),...
'Linear Fits',sprintf('t = %.3f ms',d.t(event(i,2),i))};
figname = sprintf('Run%d-Ch2-Detect-ExpWaveArrival',Cruns.Run_num(i));
tempdir = fullfile('Figures','EventDetection',...
'ExpansionWaveArrival','RefKul');
figname = fullfile(tempdir,figname);
figure('Position',[124,287,764,360]);
findchangepts(temp,'Statistic','linear')
legend(sprintf('Run $d',Cruns.Run_num(i)));
ylabel('Pressure (kPa)');
legend(templeg,'Location','best','FontSize',12);
if SavePNG; saveas(gcf,figname,'png'); end
Folded Code: Somehow this fold goes from an if statement, across a large comment block and into the middle of the next local function. I have no idea why this is happening.
if j <= 2; temp = kPa.(['Ch',num2str(j)]);
tempdir = fullfile('Figures','EventDetection',...
'ExpansionWaveArrival','RefKul');
figname = fullfile(tempdir,figname);
figure('Position',[124,287,764,360]);
findchangepts(temp,'Statistic','linear')
legend(sprintf('Run $d',Cruns.Run_num(i)));
ylabel('Pressure (kPa)');
legend(templeg,'Location','best','FontSize',12);
if SavePNG; saveas(gcf,figname,'png'); end
As of now, I haven't seen this behavior in any of my other scripts, but I haven't scoured through them all at this point. Also I hope the lack of context on the code's purpose doesn't hinder the clarity of my question.