Help using grid in nexttile and tiledlayout

17 views (last 30 days)
Angel Lozada
Angel Lozada on 11 Aug 2023
Edited: dpb on 11 Aug 2023
Hello Matlab Community
I will be appreciated if someone could help me with the follow inquiry:
After reading information about tiledlayout (https://www.mathworks.com/help/matlab/ref/tiledlayout.html) and nexttile (https://www.mathworks.com/help/matlab/ref/nexttile.html) I could not found the way to show the grid in all sub plots and show the numbers in all the sub plots (look attached file: matlab1.png) in the following code
clear;
data = readmatrix('GeoUNAM04042011L2L407-04-2011UT_Modificado_II');
n=50;
T = data(:,1);
T = seconds(data(:,1));
T.Format = 'hh:mm';
T3 = data(:,3);
m3 = movmean (T3,n);
T4 = data(:,4);
m4 = movmean (T4,n);
T5 = data(:,5);
m5 = movmean (T5,n);
T6 = data(:,6);
m6 = movmean (T6,n);
T7 = data(:,7);
m7 = movmean (T7,n);
T8 = data(:,8);
m8 = movmean (T8,n);
T9 = data(:,9);
m9 = movmean (T9,n);
T10 = data(:,10);
m10 = movmean (T10,n);
T11 = data(:,11);
m11 = movmean (T11,n);
T12 = data(:,12);
m12 = movmean (T12,n);
T13 = data(:,13);
m13 = movmean (T13,n);
T14 = data(:,14);
m14 = movmean (T14,n);
T15 = data(:,15);
m15 = movmean (T15,n);
T16 = data(:,16);
m16 = movmean (T16,n);
t = tiledlayout(7,2);
t.TileSpacing = 'tight';
t.Padding = 'tight';
ax1 = nexttile;
grid(ax1,'on')
plot (T,m3,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title ('\fontsize{6}Emitted Signal from NAA Station');
ylabel ('Phase');
%xlabel ('Samples');
ax2 = nexttile;
grid(ax2,'on')
plot (T,m4,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NAA Station';
ylabel ('Apmlitude');
% xlabel ('Samples');
nexttile
plot (T,m5,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NLK Station';
ylabel ('Phase');
% xlabel ('Samples');
nexttile
plot (T,m6,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NLK Station';
ylabel ('Amplitude');
% xlabel ('Samples');
nexttile
plot (T,m7,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NDK Station';
ylabel ('Phase');
% xlabel ('Samples');
nexttile
plot (T,m8,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NDK Station';
ylabel ('Amplitude');
% xlabel ('Samples');
nexttile
plot (T,m9,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NWC Station';
ylabel ('Phase');
% xlabel ('Samples');
nexttile
plot (T,m10,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NWC Station';
ylabel ('Amplitude');
% xlabel ('Samples');
nexttile
plot (T,m11,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NPM Station';
ylabel ('Phase');
% xlabel ('Samples');
nexttile
plot (T,m12,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NPM Station';
ylabel ('Amplitude');
% xlabel ('Samples');
nexttile
plot (T,m13,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NAU Station';
ylabel ('Phase');
% xlabel ('Samples');
nexttile
plot (T,m14,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NAU Station';
ylabel ('Amplitude');
% xlabel ('Samples');
nexttile
plot (T,m15,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NAA II Station';
ylabel ('Phase');
% xlabel ('Samples');
nexttile
plot (T,m16,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NAA II Station';
ylabel ('Amplitude');
% xlabel ('Samples');
Thanks in advance for your cooperation.

Answers (2)

Star Strider
Star Strider on 11 Aug 2023
If you want the tick labels just above the x-axis, that is certainly possible.
Try this —
data = readmatrix('GeoUNAM0404201...ficado_II.txt')
data = 510×16
0 0.0700 102.0100 26.6800 -98.5000 9.2300 -119.2600 27.9400 66.1700 16.0300 144.8000 20.3600 166.5500 1.5200 18.6400 27.3700 0.1000 0.0700 103.9800 26.7500 -93.2400 10.3700 -119.0600 27.8900 63.0800 16.0200 145.9500 19.7300 171.8900 1.4000 20.2700 27.3600 0.2000 0.0700 102.8900 26.5400 -99.9300 9.8400 -118.8500 27.8100 64.5300 16.6500 144.6300 19.9800 176.4500 2.2400 18.8900 27.2800 0.3000 0.0700 102.9800 26.5600 -101.5800 9.4500 -119.3000 27.7600 64.6700 15.8300 141.5100 19.3300 175.6900 2.2200 17.5700 27.2200 0.4000 0.0700 103.9300 26.1000 -105.8900 10.8500 -119.0200 27.7800 61.2100 15.1300 145.0100 19.2100 172.2800 2.6500 19.4600 27.2200 0.5000 0.0700 107.1800 26.3400 -103.7700 9.9800 -120.7200 27.8400 58.7200 14.9700 148.3300 19.0600 171.9700 2.6300 19.6500 27.4300 0.6000 0.0700 105.5700 26.6700 -106.9800 9.5400 -121.8800 27.7900 60.4100 15.3400 148.3500 19.2700 170.5300 2.5000 20.0100 27.2600 0.7000 0.0700 105.6900 26.5700 -115.3100 9.3600 -119.8500 27.7600 64.9700 15.1900 143.3700 19.2400 168.7100 2.8100 19.2900 27.3400 0.8000 0.0700 104.0300 26.4400 -110.3200 8.8400 -119.3600 27.8400 64.0100 15.6300 144.7900 19.2000 176.0200 3.2900 18.3600 27.4100 0.9000 -0.0600 102.5100 26.4500 -108.4600 8.6700 -120.6800 27.8600 58.7100 15.3800 148.9000 18.7100 181.6500 3.8000 17.1100 27.3300
n=50;
stn = repelem({'NAA','NLK','NDK','NWC','NPM','NAU','NAA II'}, 1, 2);
T = data(:,1);
T = seconds(data(:,1));
T.Format = 'hh:mm';
max(data(:,1))
ans = 50.9000
figure
t = tiledlayout(7,2);
t.TileSpacing = 'tight';
t.Padding = 'tight';
for k = 3:16
nexttile
plot(T, movmean(data(:,k),n))
yl = ylim;
xt = xticks;
[h,m] = hms(xt);
text(xt, zeros(size(xt))+min(yl),compose('%02d:%02d',[h m]), 'VerticalAlignment','bottom', 'HorizontalAlignment','center', 'FontSize',8)
xticks([])
yticks([])
if rem(k,2) == 0;
ylabel('Amplitude')
else
ylabel('Phase')
end
title(sprintf('\\fontsize{6}Emitted Signal from %s Station',stn{k-2}));
end
Make appropriate changes to get the result you want.
There are only 7 listed stations, so there can be only 14 plots.
The x-tick data are in seconds, and the time vector goes only up to 50.9, so all the hour and minute fields will be blank. You may want to change ‘T.Format’ to reflect that.
.

dpb
dpb on 11 Aug 2023
Edited: dpb on 11 Aug 2023
The two lines
set(gca,'YTick',[]);
set(gca,'XTick',[]);
remove the tick marks and thereby the ticklabels.
Remove those and they'll show up. You've got too much data on a plot this way, however, probably in order to be able to add for every plot and still see. But, give it a try.
As general MATLAB programming practice; don't create a zillion sequentially named variables that are just copies of the array; use indexing into the array itself and loops and a lookup table for the metadata such as the titles if it isn't available from an auxiliary data source...
Something more like
n=50;
data = readmatrix('GeoUNAM0404201...ficado_II.txt');
T=seconds(data(:,1)); T.Format='mm:ss'; % ok, I'll allow converting to duration
%[data(1,1) data(end,1)]
V1=3; NV=7; % first column variable to plot, number signals
data(:,V1:end)=movmean(data(:,V1:end),n); % do the moving mean on 'em all first
hTL = tiledlayout(NV,2); % make variable so can change mind easily
hTL.TileSpacing = 'tight';
hTL.Padding = 'tight';
YLABELS={'Phase','Ampl'}; % y labels, shorten "amplitude"
TITLES={'Emitted Signal from NAA Station'; % titles for each pair
'Emitted Signal from NLK Station';
'Emitted Signal from NDK Station';
'Emitted Signal from NWC Station';
'Emitted Signal from NPM Station';
'Emitted Signal from NAU Station';
'Emitted Signal from NAA II Station'};
% v=V1-1; % first column of pairs to plot less one
% for i=1:NV*2
% for j=1:2
% hAx=nexttile; % get a handle for current axes
% v=v+1; % increment column
% plot(T,data(:,v),'-');
% ylabel(YLABELS(j));
% hAx.FontSize=6;
% title(TITLES(i),'fontsize',6);
% end
% end
% let's try legend instead
v=V1-1; % first column of pairs to plot less one
for i=1:NV
for j=1:2
hAx=nexttile; % get a handle for current axes
v=v+1; % increment column
plot(T,data(:,v),'-');
ylabel(YLABELS(j));
hAx.FontSize=6;
station=extractBetween(TITLES(i),'from ',' Station');
%title(TITLES(i),'fontsize',6);
legend(station,'fontsize',6);
end
end
Trying to put into too little space; you'll have to see what you can do to cut things down some. You might want to consider converting to a table and looking at stackedplot, It does only one set of axes vertically, not two but you can use panels so theoretically, at least, you could put two side-by-side in a figure. It has the one advantage over either tiled layout or subplots that it uses a common x axis. Of course, one could only display the time axis here for the bottom plot and gain some space that way as one fix/help.
The basic problem is just too much; possiby dispense with the title and just use legend with only the station name without all the other verbiage. Well, forgot about the line...it takes up too much space so isn't great. I guess just an anotation/text or use it in the ylabel string for left. A global title by column for "Phase/Amplitude" would be good option, but don't think that exists except manually.

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!