tiledlayout() "exceeds array bounds" when repeated running the script, but works when workspace cleared
Mostra commenti meno recenti
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.
Risposte (1)
Walter Roberson
il 3 Giu 2023
0 voti
tiledlayout.Padding = 'compact';
That creates a struct named tiledlayout
https://www.mathworks.com/help/matlab/ref/matlab.graphics.layout.tiledchartlayout-properties.html
2 Commenti
Jianing Liu
il 6 Giu 2023
Walter Roberson
il 7 Giu 2023
No it means that you need to use
SomeVariable = tiledlayout(); SomeVariable.Padding = 'compact';
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!