How can I plot this?

조회 수: 8 (최근 30일)
Pul
Pul 2021년 9월 2일
댓글: Star Strider 2021년 9월 2일
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')

채택된 답변

Star Strider
Star Strider 2021년 9월 2일
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
Pul
Pul 2021년 9월 2일
Yes, now I got what I needed.
Thank you!
Star Strider
Star Strider 2021년 9월 2일
As always, my pleasure!
.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 9월 2일
편집: Cris LaPierre 2021년 9월 2일
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
Pul
Pul 2021년 9월 2일
Okay, thanks.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by