Graph two curves together (datetime format and double format)

Hi, thank you so much for reading me, i am using matlab 2019a, and i have this problem, i want to graph a function of curvefitting with my original curve, but i can't for this error that appears (attach photo) , i want the two graphs together .......
thank you so much for your valious time
tEnersol=readtable('solar.xlsx','sheet','EneDics'); % read into a table object
tEnersol.Properties.VariableNames={'Rad','Dia'}; % assign variable names
% yr=2014; % need the proper year
tEnersol.Date=datetime(yr,1,tEnersol.Dia); % and create a date column
hB=plot(tEnersol.Date,tEnersol.Rad, '.r'); hold on % bar plot against actual date
hAx=gca; % get axes handle...
xlim([datetime(2014,1,1) datetime(2014,12,12)]) % set limit to cover the full year
hAx.XAxis.TickLabelFormat='MM'; % show day of MONTH
% xticklabels(cellstr(tEnersol.Date(xticks))) % use the date string as tick labels
hAx.XTickLabelRotation=45; % rotate so can have chance to read...
xlabel('Mes del año, TMYE'), ylabel('Irradiancia solar máxima [KWh/m^2]');
a1= 2329
b1= 0.6522
c1= 0.95
a2= 1519
b2= 0.7781
c2= -2.545
d = (1:1:365);
w = a1.*sin(b1.*d+c1) + a2.*sin(b2.*d+c2)
plot(d,w,'.-b'); hold off

 Accepted Answer

hB=plot(tEnersol.Date,tEnersol.Rad, '.r'); hold on % bar plot against actual date
That is the first plot in the axes, and it establishes that the x axes is to be based upon datetime values.
plot(d,w,'.-b'); hold off
Your d is not datetime values, just pure numbers. You cannot plot a pure number variable in an axes that is already a datetime axes. So...
D = datetime(yr, 1, d);
plot(D, w, '.-b')

10 Comments

i obtain this (final photo) but i want something like the first photo) and thank you for your explanation
i obtain this with that code:
If you want something like the first photo, the one that results from the curvefit toolbox, then
hB = plot(tEnersol.Dia, tEnersol.Rad, '.r'); hold on % bar plot against day of year
xtick(0:50:350);
i tried it but this happens.
Why are you using d = 1:50:365 ? That is going to give you a very rough w line. Why are you not using d=1:365 ?
thank you , i finally use this code:
tEnersol=readtable('solar.xlsx','sheet','EneDics'); % read into a table object
tEnersol.Properties.VariableNames={'Rad','Dia'}; % assign variable names
yr=2014; % need the proper year
tEnersol.Date=datetime(yr,1,tEnersol.Dia); % and create a date column
hB=plot(tEnersol.Date,tEnersol.Rad, '.r'); hold on % plot against actual date
% xticks(0:50:365);
hAx=gca; % get axes handle...
xlim([datetime(2014,1,1) datetime(2014,12,12)]) % set limit to cover the full year
hAx.XAxis.TickLabelFormat='MM'; % show day of MONTH
% xticklabels(cellstr(tEnersol.Date(xticks))) % use the date string as tick labels
hAx.XTickLabelRotation=45; % rotate so can have chance to read...
xlabel('Mes del año, TMYE'), ylabel('Irradiancia solar máxima [KWh/m^2]');
% z = fminsearch(@ds,[1339 .05 .12 492.1 .01 1.96])
% fplot(a1.*sin(b1.*d+c1) + a2.*sin(b2.*d+c2), [0,365]) hold off
a1= 1339
b1= 0.00571
c1= 0.1232
a2= 492.1
b2= 0.0086
c2= 1.966
d =(1:365);
w = a1.*sin(b1.*d+c1) + a2.*sin(b2.*d+c2)
D = datetime(yr, 1, d);
plot(D, w, '.-b')
and obtain this (attach photo) i can put the equation in the figure ?
thank you so much
text() or legend() to put the equation in. You will probably want to use 'Interpreter', 'latex'

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!