Changing x-axis following use of fit and plot
Afficher commentaires plus anciens
I am using fit as described in https://www.mathworks.com/help/curvefit/fit.html to fit and plot a timestamp (in seconds from a base epoch) vs. data along with the fit and confidence values. I can't copy/paste results unforunately so this will have to be descriptive.
% timestamp = 1d array of doubles: seconds elapsed since 1/1/1970; ~1.57E9 sec
% data = 1d array of doubles: one data point per timestamp point; ~1E-10
curvefit = fit(timestamp, data, 'poly1', 'normalize','on'); % Generate fit
plot(curvefit, timestamp, data, 'o', 'predfunc') ; % Generate plot showing data, fit, and confidence bounds
For the plot, I'd like the x-axis to be dates. I can convert the timestamp to a datestamp using
datestamp = datetime(timestamp, 'ConvertFrom', 'epochtime', 'Epoch', epoch) ; % with epoch defined elsewhere
I tried getting the axis limits for the gcf and updating:
lim = axis;
axis( [datestamp(1) datestamp(end) lim(3) lim(4)] ); % Change x-axis, keep y-axis
The error message says all inputs must be datetimes, date/time character vectors, or date/time strings.
xlim([datestamp(1) datestamp(end)])
The error message says that xlim requires numeric inputs.
Interestingly, I can generate a data vs. datestamp plot:
plot(datestamp, data,'o')
but this does not have the benefit of autogenerated fits, confidence bounds, and legends.
I'm missing something simple...
7 commentaires
dpb
le 19 Oct 2019
What's a timestamp? fit(x,y,...) x is according to above link
Data to fit, specified as a matrix with either one (curve fitting) or two (surface fitting) columns. You can specify variables in a MATLAB table using tablename.varname. ...
...
Data Types: double
OldGuyInTheClub
le 19 Oct 2019
OK, you fitted and plotted against a double value...you can convert the time into datetimes or durations; you must then plot using that as the x argument in plot() for it to recognize the x-axis is to be a datetimeruler object. Once you do that, THEN you can set limits as you wish, but you can't set limits on a double axis in terms of datetime variables as you've tried to do nor can you set the limits on a plot with datetimes as the axis values with doubles--they must be self-consistent.
plot() has versions that recognize the fit() returned object--problem is in your case that there isn't a version of fit() that recognizes datetime; only doubles. Two ways to proceed:
- Use the venerable datenum instead of datetime for the fit function time input. It is just a double underneath. When plot() with it, then must use datetick to set the display of the axis to time units. Klunky, but can be done.
- Use a second axis overlaying the first and plot the data against the datetime result on it, turning the axes of the first off so see the time values displayed, not the underlying seconds as double.
OldGuyInTheClub
le 21 Oct 2019
Modifié(e) : OldGuyInTheClub
le 21 Oct 2019
dpb
le 21 Oct 2019
"Do you mean to do the appropriate conversion of datenum(datestamp) into seconds and pass that to fit?"
Sorta', but not quite...you have to pick an (can be arbitrary) reference date for datenum as well as the input seconds and then fit using it if you want to continue to use fit for its extra features of automagically returning the additional info such as confidence limits.
Since you're actually only interested in a duration from that initial time, in actuality you could then pass the difference after subtracting the initial time leaving the seconds portion only, just has have done with seconds excepting with a scaling factor.
With datenum, the integer portion is date while the fractional is time (expressed as fraction of 24-hr day). Then when you plot, you'll get either a very big number on x axis initially if you use full value for fitting or a fractional day range if use the difference. Either way, you can set the format to whatever form you choose with datetick.
For the second, make the two axes overlay
hAx(2)=axes('Position', hAx(1), 'Color','none');
hAx(1).XTick=[]; % don't have ticks on first
plot(hAx(2),datestamp,data)
should work.....it's what plotyy does, in essence.
OldGuyInTheClub
le 21 Oct 2019
OldGuyInTheClub
le 21 Oct 2019
Modifié(e) : OldGuyInTheClub
le 21 Oct 2019
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Calendar dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!