tiledlayout() "exceeds array bounds" when repeated running the script, but works when workspace cleared

2 views (last 30 days)
Hi,
I called tiledlayout() to create a 3x2 figure containing 6 subplots. I used nexttile to navigate to next subplots. Notice that I already specificed which grid to plot in by putting the index argument like nexttile(n_target_grid). When I have generated the figure by running the script once, and then running it again (with some fixes applied) the following error would occur:
Index in position 1 exceeds array bounds. Index must not exceed 1.
Error in HW3_Ex2 (line 66)
tiledlayout(3, 2);
If I manually clear the workspace, the script would run fine; however, I would like to know specifcially which variable or operation went wrong, and if there is a way to avoid repeatedly clearning the workspace everytime (especially since MATLAB suggested not to do to to improve efficiency). I suspect there is some kind of "cursor" used by the tiledlayout() that went out of bound. Is there a way to "return" it to the starting position, and let the new run of script overwrite what was in there already?
Below is the plotting part of the script:
%% Find FourierCoefficients of them
DFC_square = FindFourierCoeff(clipped_y_square);
DFC_tri = FindFourierCoeff(clipped_y_tri);
DFC_para = FindFourierCoeff(clipped_y_para);
% Compute the frequency resolution
df = fs_uni / length(DFC_para);
% Define the x-axis values as the actual frequencies
frequencies = (0:length(DFC_para)-1) * df;
%% Plot amplitude of real and phase
figure;
tiledlayout(3, 2);
% Plot the absolute amplitudes of the real parts in the first column
nexttile(1);
stem(frequencies, abs(real(DFC_square)));
title('Square Wave - Real Part (Amplitude)');
xlabel('Frequency');
ylabel('Amplitude');
nexttile(3);
stem(frequencies, abs(real(DFC_tri)));
title('Triangular Wave - Real Part (Amplitude)');
xlabel('Frequency');
ylabel('Amplitude');
nexttile(5);
stem(frequencies, abs(real(DFC_para)));
title('Parabolic Wave - Real Part (Amplitude)');
xlabel('Frequency');
ylabel('Amplitude');
% Plot the phases in the second column
nexttile(2);
stem(frequencies, angle(DFC_square));
title('Square Wave - Phase');
xlabel('Frequency');
ylabel('Phase (Radians)');
nexttile(4);
stem(frequencies, angle(DFC_tri));
title('Triangular Wave - Phase');
xlabel('Frequency');
ylabel('Phase (Radians)');
nexttile(6);
stem(frequencies, angle(DFC_para));
title('Parabolic Wave - Phase');
xlabel('Frequency');
ylabel('Phase (Radians)');
% Adjust the layout spacing
spacing = 0.05;
tiledlayout.Padding = 'compact';
tiledlayout.TileSpacing = spacing;
This is indeed part of a data analysis homework, but this question is not related to the core assessment of the homework but rather matlab usage, so I hope it's okay. Thanks to everyone.

Answers (1)

Walter Roberson
Walter Roberson on 3 Jun 2023
tiledlayout.Padding = 'compact';

That creates a struct named tiledlayout

https://www.mathworks.com/help/matlab/ref/matlab.graphics.layout.tiledchartlayout-properties.html

  2 Comments
Jianing Liu
Jianing Liu on 6 Jun 2023
Thank you for the reply! Does that mean I should manually delete this struct at the second+ time of running the script? Or is there an alternative approach to let it overrite the struct generated last time?

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!