When plotting multiple subplots in the for loop, how do you stop the axes from overlapping?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to plot the suplots in the for loop using the following code:
for k = 1:length(txt_files)
% some code
if PWM_switch == 1
ax1 = axes(t);
ax1.Layout.Tile = Test_Case;
if mod_switch == 1
if OpenLoop == 0
plot(table.data(:,1), table.data(:,2), 'k'); hold on;
elseif OpenLoop == 1
plot(table.data(:,1), table.data(:,2), 'k--'); hold on;
end
end
if mod_switch == 4
if OpenLoop == 0
plot(table.data(:,1), table.data(:,2), 'r'); hold on;
elseif OpenLoop == 1
plot(table.data(:,1), table.data(:,2), 'r--'); hold on;
end
end
end
end
but it appears that every time ax1 = axes(t) is executed, the supplot gets overlapping. You can still see old y-axis scales, but only the latest plots. When I run the loop step-by-step, all plots are plotted in the correct subplots; only they are overlapping.
I need to switch between subplots based on the conditions, i.e., Test Cases.
ax1.Layout.Tile = Test_Case correctly locates the subplot.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1729421/image.jpeg)
The full code with all *.txt files is in the attachment.
0 commentaires
Réponses (2)
Walter Roberson
le 7 Juil 2024
Modifié(e) : Walter Roberson
le 7 Juil 2024
ax1 = axes(t);
That either creates a new axes for the figure number or figure handle is given in t, or else actives the axes whose axes handle (or axes number) is given in t.
My guess is that t is being interpreted as a figure number... and then the tiles are being arranged independently of whatever axes happens to be underlaying at the time.
0 commentaires
Voss
le 7 Juil 2024
Modifié(e) : Voss
le 7 Juil 2024
axes(t) where t is a tiledlayout, creates a new axes in t, so your code is creating a new axes on each iteration of the loop. That's why there are so many overlapping labels. I think the intent of the code is to plot into existing axes when one already exists in the necessary Tile. To do that, you can use some logic like below:
unzip TestCode.zip
cd TestCode
% Define the directory containing the .mat files
txt_files_directory = pwd; % Change this to your directory path
% Get a list of all .mat files in the directory
txt_files = dir(fullfile(txt_files_directory, '*.txt'));
set(0,'DefaultLineLineWidth', 2.5)
set(0, 'DefaultAxesFontName', 'Times New Roman')
set(0, 'DefaultAxesFontSize', 20)
fig = figure('visible','on');
t = tiledlayout(1,5,'TileSpacing','tight','Padding', 'tight');
% 1-by-5 vector of graphics objects, to be populated with axes when they
% are created in the loop:
ax = gobjects(1,5);
for k = 1:length(txt_files)
% Given string
str = txt_files(k).name;
% Extract numbers using regular expression
numbers = regexp(str, '\d+', 'match');
% Convert to numerical values
numbers = str2double(numbers);
% Assign values to variables
PWM_switch = numbers(1); % First number (2)
mod_switch = numbers(2); % Second number (1)
Test_Case = numbers(3); % Third number (1)
OpenLoop = numbers(4); % Fourth number (1)
% Load .txt file
filename = str;
delimiterIn = ',';
headerlinesIn = 1;
table = importdata(filename,delimiterIn,headerlinesIn);
if PWM_switch == 1
% check whehter the axes in location Test_Case exists yet
if ~ishandle(ax(Test_Case))
% if not, create it
ax(Test_Case) = axes(t);
% and set its Tile location
ax(Test_Case).Layout.Tile = Test_Case;
end
% now ax(Test_Case) is guaranteed to exist, so make it the
% CurrentAxes (you could also store ax1=ax(Test_Case) and then plot
% into ax1 without making it the CurrentAxes using plot(ax1,__)
axes(ax(Test_Case));
if mod_switch == 1
if OpenLoop == 0
plot(table.data(:,1), table.data(:,2), 'k'); hold on;
elseif OpenLoop == 1
plot(table.data(:,1), table.data(:,2), 'k--'); hold on;
end
end
if mod_switch == 4
if OpenLoop == 0
plot(table.data(:,1), table.data(:,2), 'r'); hold on;
elseif OpenLoop == 1
plot(table.data(:,1), table.data(:,2), 'r--'); hold on;
end
end
end
end
Note that axes(obj), where obj is the parent of the axes, as in axes(t), creates a new axes, but axes(obj) where obj is an existing axes does not create a new axes and instead makes obj the CurrentAxes of the (ui)figure. This is explained here:
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!