Why do I get this error while plotting this?

Hello everyone,
I would like to plot the first plot you can see here with the "%other plots" that you see in the code, but I got an error.
Can anyone help me please?
Thank you.
load('giulia_TT');
GIULIA_YEARLY_N=retime(giulia_TT,'yearly','mean');
N=GIULIA_YEARLY_N.Var5*3.5
N = 23×1
NaN 57.2138 97.4158 147.7474 171.8935 146.9554 203.3139 238.9639 266.8034 344.4977
LD = load('giulia_TT.mat');
giulia_TT = LD.giulia_TT;
GIULIA_YEARLY_N = retime(giulia_TT,'yearly', @(x)mean(x,'omitnan')); % Yearly Average
N=GIULIA_YEARLY_N.Var5*3.5;
SelIdx = year(GIULIA_YEARLY_N.Time) >= 1998; % Logical Mask Vectror
N_Sel = N(SelIdx);
N_Sel_Dif = -diff(N_Sel);
Years = 1998+(1:numel(N_Sel_Dif));
figure
plot(Years, N_Sel_Dif)
grid
xlim([min(Years) max(Years)])
xlabel('Years')
ylabel('Yearly N-Differences')
set(gca, 'XTick',Years)
plot(Years, N_Sel_Dif,'g-^' ,'MarkerEdgeColor','k','MarkerFaceColor','g')
hold on
set(gca, 'ytick', -200:25:400);
%OTHER PLOTS
load('PALINE_GIULIA_DELTA');
errorbar(T.Year(:,1),T.Var4(:,1),T.Var3(:,1),'.r','MarkerSize',6)
Error using errorbar (line 185)
Data inputs must match the axis configuration. A numeric axis must have numeric data inputs or data inputs which can be converted to double.
hold on
% plot(T.Year(:,1),[NEW(:,1), T.Var4(:,1)]);
hold on
xlabel('Time');
ylabel('Height(mm)');
title('Giulia AWS and Stakes')
%error bar with different colors
errorbar(T.Year(1:3696,1),T.Var4(1:3696,1),T.Var3(1:3696,1),'.r','MarkerSize',6,'DisplayName','Stakes'); hold on
hold on
errorbar(T.Year(4406:4771,1),T.Var4(4406:4771,1),T.Var3(4406:4771,1),'.b','MarkerSize',6,'DisplayName','Stakes');
hold on
errorbar(T.Year(4772:4880,1),T.Var4(4772:4880,1),T.Var3(4772:4880,1),'.r','MarkerSize',6);
hold on
errorbar(T.Year(4881:7722,1),T.Var4(4881:7722,1),T.Var3(4881:7722,1),'.b','MarkerSize',6);
hold on
%plot(T.Year(:,1),[NEW, T.Var4(:,1)],'g');
xlabel('Time');
ylabel('Height(mm)');
title('Giulia');
load('DATI_MAR_ANNUALI');
load('DATA_ECM');
plot(datetime(DATIMARannuali.Year(20:end,1),1,1), DATIMARannuali.SMB_mp_mm(20:end,1),'--co', 'MarkerEdgeColor','k','MarkerFaceColor','c', 'DisplayName','MAR');
hold off
hold on
plot(datetime(DATIECMWFannuali.Year,1,1), DATIECMWFannuali.SMB_mp_mm,'m-*', 'DisplayName','ECMWF');
legend('Location','best')
hold on

 채택된 답변

Walter Roberson
Walter Roberson 2021년 8월 26일

0 개 추천

errorbar() does support datetime inputs
The problem is that you are plotting year first, and the year you plot is pure numeric, 1998+something. Once you have plotted numeric in an axes, you cannot plot datetime or categorical in the same axes.
The workaround is to turn the year into datetime objects,
Years = datetime(Years, 1, 1, 'Format', 'yyyy')

댓글 수: 8

Pul
Pul 2021년 8월 26일
Thank you.
I don't get how I could change it.
load('giulia_TT');
GIULIA_YEARLY_N=retime(giulia_TT,'yearly','mean');
N=GIULIA_YEARLY_N.Var5*3.5
LD = load('giulia_TT.mat');
giulia_TT = LD.giulia_TT;
GIULIA_YEARLY_N = retime(giulia_TT,'yearly', @(x)mean(x,'omitnan')); % Yearly Average
N=GIULIA_YEARLY_N.Var5*3.5;
SelIdx = year(GIULIA_YEARLY_N.Time) >= 1998; % Logical Mask Vectror
N_Sel = N(SelIdx);
N_Sel_Dif = -diff(N_Sel);
Years = 1998+(1:numel(N_Sel_Dif));
Years = datetime(Years, 1, 1, 'Format', 'yyyy')
figure
plot(Years, N_Sel_Dif)
grid
xlim([min(Years) max(Years)])
xlabel('Years')
ylabel('Yearly N-Differences')
set(gca, 'XTick',Years)
plot(Years, N_Sel_Dif,'g-^' ,'MarkerEdgeColor','k','MarkerFaceColor','g')
hold on
set(gca, 'ytick', -200:25:400);
%OTHER PLOTS
load('PALINE_GIULIA_DELTA');
TYear = datetime(T.Year, 1, 1, 'Format', 'yyyy');
errorbar(Tyear,T.Var4(:,1),T.Var3(:,1),'.r','MarkerSize',6)
hold on
% plot(T.Year(:,1),[NEW(:,1), T.Var4(:,1)]);
hold on
xlabel('Time');
ylabel('Height(mm)');
title('Giulia AWS and Stakes')
%error bar with different colors
errorbar(TYear(1:3696,1),T.Var4(1:3696,1),T.Var3(1:3696,1),'.r','MarkerSize',6,'DisplayName','Stakes'); hold on
hold on
errorbar(TYear(4406:4771,1),T.Var4(4406:4771,1),T.Var3(4406:4771,1),'.b','MarkerSize',6,'DisplayName','Stakes');
hold on
errorbar(TYear(4772:4880,1),T.Var4(4772:4880,1),T.Var3(4772:4880,1),'.r','MarkerSize',6);
hold on
errorbar(TYear(4881:7722,1),T.Var4(4881:7722,1),T.Var3(4881:7722,1),'.b','MarkerSize',6);
hold on
%plot(T.Year(:,1),[NEW, T.Var4(:,1)],'g');
xlabel('Time')
ylabel('Height(mm)');
title('Giulia');
load('DATI_MAR_ANNUALI');
load('DATA_ECM');
DYMAR = datetime(DATIMARannuali.Year,1,1)
plot(DYMAR(20:end), DATIMARannuali.SMB_mp_mm(20:end,1),'--co', 'MarkerEdgeColor','k','MarkerFaceColor','c', 'DisplayName','MAR');
hold off
hold on
DYCMWF = datetime(DATIECMWFannuali.Year,1,1);
plot(DYCMWF, DATIECMWFannuali.SMB_mp_mm,'m-*', 'DisplayName','ECMWF');
legend('Location','best')
hold on
Thanks for your help.
But here
"TYear = datetime(T.Year, 1, 1, 'Format', 'yyyy');",
I got this error:"Invalid parameter name. Parameter name must be a nonempty string or character vector."
DGM
DGM 2021년 8월 26일
편집: DGM 2021년 8월 26일
Oh. That must be a recent change, since it doesn't seem to in R2019b.
EDIT: I guess that's new in R2021a
TYear = T.Year; TYear.Format = 'yyyy';
errorbar(TYear, T.Var4(:,1), T.Var3(:,1), '.r', 'MarkerSize', 6)
@Walter Roberson Thank you, but I'm always having the same error:
errorbar(TYear(4881:7722,1),T.Var4(4881:7722,1),T.Var3(4881:7722,1),'.b','MarkerSize',6);
"Data inputs must match the axis configuration. A numeric axis must have numeric data inputs or data inputs which can be converted to double."
I do not encounter any problem.
load('giulia_TT');
LD = load('giulia_TT.mat');
giulia_TT = LD.giulia_TT;
GIULIA_YEARLY_N = retime(giulia_TT,'yearly', @(x)mean(x,1,'omitnan')); % Yearly Average
N=GIULIA_YEARLY_N.Var5*3.5;
SelIdx = year(GIULIA_YEARLY_N.Time) >= 1998; % Logical Mask Vectror
N_Sel = N(SelIdx);
N_Sel_Dif = -diff(N_Sel);
Years = 1998+(1:numel(N_Sel_Dif));
Years = datetime(Years, 1, 1, 'Format', 'yyyy');
figure
plot(Years, N_Sel_Dif)
grid
xlim([min(Years) max(Years)])
xlabel('Years')
ylabel('Yearly N-Differences')
set(gca, 'XTick',Years)
plot(Years, N_Sel_Dif,'g-^' ,'MarkerEdgeColor','k','MarkerFaceColor','g')
hold on
set(gca, 'ytick', -200:25:400);
%OTHER PLOTS
load('PALINE_GIULIA_DELTA');
TYear = T.Year; TYear.Format = 'yyyy';
errorbar(TYear, T.Var4(:,1), T.Var3(:,1), '.r', 'MarkerSize', 6)
hold on
% plot(T.Year(:,1),[NEW(:,1), T.Var4(:,1)]);
hold on
xlabel('Time');
ylabel('Height(mm)');
title('Giulia AWS and Stakes')
%error bar with different colors
errorbar(TYear(1:3696,1),T.Var4(1:3696,1),T.Var3(1:3696,1),'.r','MarkerSize',6,'DisplayName','Stakes'); hold on
hold on
errorbar(TYear(4406:4771,1),T.Var4(4406:4771,1),T.Var3(4406:4771,1),'.b','MarkerSize',6,'DisplayName','Stakes');
hold on
errorbar(TYear(4772:4880,1),T.Var4(4772:4880,1),T.Var3(4772:4880,1),'.r','MarkerSize',6);
hold on
errorbar(TYear(4881:7722,1),T.Var4(4881:7722,1),T.Var3(4881:7722,1),'.b','MarkerSize',6);
hold on
%plot(T.Year(:,1),[NEW, T.Var4(:,1)],'g');
xlabel('Time')
ylabel('Height(mm)');
title('Giulia');
load('DATI_MAR_ANNUALI');
load('DATA_ECM');
DYMAR = datetime(DATIMARannuali.Year,1,1);
plot(DYMAR(20:end), DATIMARannuali.SMB_mp_mm(20:end,1),'--co', 'MarkerEdgeColor','k','MarkerFaceColor','c', 'DisplayName','MAR');
hold off
hold on
DYCMWF = datetime(DATIECMWFannuali.Year,1,1);
plot(DYCMWF, DATIECMWFannuali.SMB_mp_mm,'m-*', 'DisplayName','ECMWF');
legend('Location','best')
hold on
Pul
Pul 2021년 8월 27일
Now it works, thank you.

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

추가 답변 (1개)

DGM
DGM 2021년 8월 25일

0 개 추천

errorbar() doesn't support datetime inputs. You'll end up needing to avoid that, which might complicate things a bit:

댓글 수: 1

Pul
Pul 2021년 8월 26일
편집: Pul 2021년 8월 27일
but it's weird, because on my matlab I can plot this plots separately, but not all together.

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

카테고리

도움말 센터File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

질문:

Pul
2021년 8월 25일

편집:

Pul
2021년 8월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by