MATLAB Answers

How can I plot this?

1 view (last 30 days)
Pul
Pul on 2 Sep 2021
Commented: Star Strider on 2 Sep 2021
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

Star Strider
Star Strider on 2 Sep 2021
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
Star Strider
Star Strider on 2 Sep 2021
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.
  1 Comment
Pul
Pul on 2 Sep 2021
Okay, thanks.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!