Error while sub plotting

1 次查看(过去 30 天)
John Carroll
John Carroll 2021-2-24
Hello I am trying to plot 6 subplot to one figure. However when i run my code i am reciving a figure with only one plot witch is empty. Along with the plots not having any data the axis are not visble either. Any help would be much appricated.
My code as follows:
Y2 = readtable('2hz000.xlsx','Range','E1:AJ11');
twoHZ = table2array(Y2);
two = detrend(twoHZ);
for i=1:32
[pks] = findpeaks(two(:,i));
meanWH(i) = mean(pks);
end
mn1 = mean(meanWH);
WH2 = mn1*2
Y4 = readtable('4hz000.xlsx','Range','E1:AJ21');
fourHZ = table2array(Y4);
four = detrend(fourHZ);
%not enough points to get waveheight
Y8 = readtable('8hz000.xlsx','Range','E1:AJ48');
eightHZ = table2array(Y8);
eight = detrend(eightHZ);
for i=1:32
[pks] = findpeaks(eight(:,i));
meanWH(i) = mean(pks);
end
mn1 = mean(meanWH);
WH8 = mn1*2
Y16 = readtable('16hz000.xlsx','Range','E1:AJ86');
sixteenHZ = table2array(Y16);
sixteen = detrend(sixteenHZ);
for i=1:32
[pks] = findpeaks(sixteen(:,i));
meanWH(i) = mean(pks);
end
mn1 = mean(meanWH);
WH16 = mn1*2
Y32 = readtable('32hz000.xlsx','Range','E1:AJ181');
thirtytwoHZ = table2array(Y32);
thirtytwo = detrend(thirtytwoHZ);
for i=1:32
[pks] = findpeaks(thirtytwo(:,i));
meanWH(i) = mean(pks);
end
mn1 = mean(meanWH);
WH32 = mn1*2
X2 = (0:0:10);
X4 = (0:0:20);
X8 = (0:0:47);
X16 = (0:0:85);
X32 = (0:0:180);
figure (1)
subplot(2,3,1)
plot(X2,twoHZ,'b--');
xlabel('time(sec)')
ylabel('height (m)')
title('Two Hz')
subplot(2,3,2)
plot(X4,fourHZ,'g--');
xlabel('time(sec)')
ylabel('height (m)')
title('Four Hz')
subplot(2,3,3)
plot(X8,eightHZ,'r--');
xlabel('time(sec)')
ylabel('height (m)')
title('Eight Hz')
subplot(2,3,4)
plot(X16,sixteenHZ,'r--');
xlabel('time(sec)')
ylabel('height (m)')
title('Sixteen Hz')
subplot(2,3,5)
plot(X32,thirtytwoHZ,'r--');
xlabel('time(sec)')
ylabel('height (m)')

回答(1 个)

Just Manuel
Just Manuel 2021-2-24
编辑:Just Manuel 2021-2-24
your problem lies here:
X2 = (0:0:10);
X4 = (0:0:20);
X8 = (0:0:47);
X16 = (0:0:85);
X32 = (0:0:180);
when you specify zero as the element spacing with the column operator, you end up with an empty row vector.
You did not mention that matlab complained about "Vectors must be the same length.", so i expect that also twoHZ, etc. are empty.
However, I suspect, matlab did give you that error and you just forgot to tell us :)
figure (1)
Creates your figure window, while
subplot(2,3,1)
creates the empty plot.
plot(X2,twoHZ,'b--');
will then produce the error and abort execution of your script. This is why you end up with only one empty plot.
Cheers
Manuel

Community Treasure Hunt

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

Start Hunting!

Translated by