How can I plot this?

Hello everyone,
I should plot the magenta data (Cum_smbp.SMB_mpmm) until 30/1/2019.
I tried in this way: plot(DTv,table2array(Cum_smbp(:,16),Cum_smbp.SMB_mpmm(1:7700),'m', 'DisplayName')), without success.
Can anyone help me please?
Thanks.
load('GIULIA_MMEQ1.mat');
A=GIULIAMMEQ1.Var4;
B=str2double(A);
NEW= B * 10 * 0.35;
C=GIULIAMMEQ1.Dec1997;%array2table
C=replace(C,"';","");
C=datetime(C,'InputFormat','dd MMM yyyy'); %convert to datetime format
plot(C,NEW)
load('DATI_ECM_GIORNALIERI')
DTv = datetime(DATIECMWFgiornalieri{:,1:3})
smb=table2array(DATIECMWFgiornalieri(:,16))
for i =1:8402
if isnan(smb(i))
smb(i)=0;
end
end
Cum=cumsum(smb)
Cum_smbp=DATIECMWFgiornalieri;
Cum_smbp(:,16)=array2table(Cum)
plot(C,NEW, 'DisplayName')
hold on
plot(DTv,table2array(Cum_smbp(:,16)),'m', 'DisplayName');
plot(DTv,table2array(Cum_smbp(:,16),Cum_smbp.SMB_mpmm(1:7700),'m', 'DisplayName'));
legend('Location','best')

 Accepted Answer

Try this (slightly edited version) —
load('GIULIA_MMEQ1.mat');
A=GIULIAMMEQ1.Var4;
B=str2double(A);
NEW= B * 10 * 0.35;
C=GIULIAMMEQ1.Dec1997;%array2table
C=replace(C,"';","");
C=datetime(C,'InputFormat','dd MMM yyyy'); %convert to datetime format
plot(C,NEW)
load('DATI_ECM_GIORNALIERI.mat')
DTv = datetime(DATIECMWFgiornalieri{:,1:3});
smb=table2array(DATIECMWFgiornalieri(:,16));
smb = fillmissing(smb, 'constant',0);
% for i =1:8402
% if isnan(smb(i))
% smb(i)=0;
% end
% end
Cum=cumsum(smb);
Cum_smbp=DATIECMWFgiornalieri;
Cum_smbp(:,16)=array2table(Cum);
plot(C,NEW, 'DisplayName','First Plot')
hold on
plot(DTv,table2array(Cum_smbp(:,16)),'m', 'DisplayName','Second Plot');
plot(DTv,table2array(Cum_smbp(:,16),Cum_smbp.SMB_mpmm(1:7700)),'m', 'DisplayName','Third Plot');
legend('Location','best')
producing:
.

6 Comments

Pul
Pul on 2 Sep 2021
Thanks, but I get the same graph!
I need that magenta data (in the plot) end on the same day (30/1/2019) of the blue data.
My pleasure!
I need that magenta data (in the plot) end on the same day (30/1/2019) of the blue data.
I do not understand.
What I labeled as the ‘Second Plot’ and ‘Third Plot’ (because the value for the name-value pair 'DisplayName' was missing for all of them) are both magenta in your code.
Which one do you want to be different, and how do you want them to be different?
Do you just want to plot one point (or one day if there arre more than one point in a day) for ‘30/1/2019?
.
Pul
Pul on 2 Sep 2021
I want to plot just this one: plot(DTv,table2array(Cum_smbp(:,16),Cum_smbp.SMB_mpmm(1:7700)),'m', 'DisplayName','Third Plot').
[I put (1:7700) because it's 30/1/2019 for the magenta data.]
Shortly, I'd like that the magenta data plot(DTv,table2array(Cum_smbp(:,16),Cum_smbp.SMB_mpmm(1:7700)),'m', 'DisplayName','Third Plot') end in the same day in which blue data end, which is 30/1/2019.
In this way, I can compare both in the same time span.
Thank you!
My pleasure!
Try this —
DTv = datetime(DATIECMWFgiornalieri{:,1:3});
Lv = DTv <= datetime('30-Jan-2019'); % Logical Vector Restricting Range
smb=table2array(DATIECMWFgiornalieri(:,16));
smb = fillmissing(smb, 'constant',0);
% for i =1:8402
% if isnan(smb(i))
% smb(i)=0;
% end
% end
Cum=cumsum(smb);
Cum_smbp=DATIECMWFgiornalieri;
Cum_smbp(:,16)=array2table(Cum);
figure
plot(C,NEW, 'DisplayName','First Plot')
% hold on
% plot(DTv,table2array(Cum_smbp(:,16)),'m', 'DisplayName','Second Plot');
plot(DTv(Lv),table2array(Cum_smbp(Lv,16),Cum_smbp.SMB_mpmm(1:7700)),'m', 'DisplayName','Third Plot');
legend('Location','best')
The code before that is unchanged, so there was no reason to post it, or the new plot (that appears to me to be correct).
.
Pul
Pul on 2 Sep 2021
Yes, now I got what I needed.
Thank you!
As always, my pleasure!
.

Sign in to comment.

More Answers (1)

Cris LaPierre
Cris LaPierre on 2 Sep 2021
Edited: Cris LaPierre on 2 Sep 2021
There are some inconsistencies in your code. The issue with the line you are questioning is that you have one X input and 2 Y inputs. You can plot multiple lines in a single plot command, but you must use the correct syntax.
Also note that DTv has 8402 rows and Cum_smbp.SMB_mpmm(1:7700) will have 7700, so you have more X data points than Y data points. This will give you an error.
You can use a logical array to identify data that meets a conditional requirement. See Ch 12 of MATLAB Onramp on how to do this. Use that array as an index to select the data to plot.
x=1:5;
y=x.^2;
ind = y<10
ind = 1×5 logical array
1 1 1 0 0
plot(x(ind),y(ind),'m-o')
Some other things
  • 'DisplayName' is a Name-Value pair input. You need to include both (e.g. plot(C,NEW, 'DisplayName','Line1')
  • It is best practice to always pair hold on with a corresponding hold off
  • There is no need to do table2array(Cum_smbp(...)). See the Access Data in Tables page for more.

Asked:

Pul
on 2 Sep 2021

Commented:

on 2 Sep 2021

Community Treasure Hunt

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

Start Hunting!