stacked plot with 2 time series of different length and spacing

6 次查看(过去 30 天)
I have 2 time series. Both are different lengths (but they overlap for the first 1300s), and are sampled at different rates. Is there a way to plot this information on a stacked plot?
I want to be able to show the periodicity in the flucctuations so dont really want to plot them in subplot form.
files = dir('*.txt');
N = length(files);
A = cell(1,N);
A2 = cell(1,N);
for ii = 1:max(size(files));
if files(ii).isdir ~=true
fname = files(ii).name;
file = fopen(fname);
A{ii} = cell2mat(textscan(file, '%f %f %f'));
fclose(file);
end
[~,idx] = unique(A{ii}(:,1));
A2{ii} = A{ii}(idx,:);
end
plot(A2{1}(:,2),A2{1}(:,3))
hold on
plot(A2{2}(:,2),A2{2}(:,3))
xlabel('Time (s)')

回答(1 个)

Star Strider
Star Strider 2024-7-1
There is a way to use stackedplot with them, however it requires that they be interpolated to the same x-axis vector —
files = dir('*.txt');
for k = 1:numel(files)
A{k} = readmatrix(files(k).name);
Arows(k) = size(A{k},1);
[A11(k),A12(k)] = bounds(A{k}(:,1));
end
A{:}
ans = 544x3
0.2000 2.7120 2.8200 0.3050 8.0307 2.8700 0.4300 13.4546 3.6000 0.5150 17.1429 4.3300 0.5900 20.6028 4.4900 0.7900 31.4507 4.2400 0.8950 37.1459 3.8100 0.9900 42.5087 4.2200 1.0950 49.2020 4.2300 1.1200 51.0098 3.9900
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 238x3
0.2000 4.2758 3.3500 0.3000 12.1207 4.2100 0.3900 18.4630 4.4600 0.5100 26.5427 4.6300 0.6200 33.6109 4.7200 0.7100 39.2466 4.4100 0.7800 43.1440 4.5500 0.8900 49.2685 4.6100 1.0000 55.3930 4.6400 1.1000 60.9607 4.5500
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[Amin,Amax] = bounds([A11(:); A12(:)])
Amin = 0.2000
Amax = 59.5600
figure
tiledlayout(2,1)
for k = 1:numel(A)
nexttile
semilogy(A{k}(:,1), A{k}(:,2:end))
xlim([Amin, Amax])
grid
end
sgtitle('Using ‘tiledlayout’')
xc = linspace(Amin, Amax, max(Arows));
for k = 1:numel(A)
[A1u,ix] = unique(A{k}(:,1),'stable');
Ay{k} = interp1(A{k}(ix,1), A{k}(ix,2:end), xc);
end
figure
stackedplot(xc, cell2mat(Ay))
grid
sgtitle('Using ‘stackedplot’ & Interpolation')
I would prefer tiledlayout for this, however stackedplot is certainly possible.
.

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by