MATLAB Answers

How to display the contour line values from a contour plot generated using the curve fitting tools?

24 views (last 30 days)
I created this 2D contour plot using sfit from the curve fitting tools app, and I am trying to have the values of each contour line displayed in the contour plot. I've gone through some other examples, but they only use the contour function, and not data generated from the sfit. Here is the generic code extracted from the CFtools:
%CREATEFIT(SCANTIME,DRUMHEIGHT,CORRECTEDDENSITY)
% Create a fit.
%
% Data for 'untitled fit 1' fit:
% X Input : Time
% Y Input : Location
% Z Output: Speed
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 29-Mar-2017 23:17:04
%%Fit: 'untitled fit 1'.
[xData, yData, zData] = prepareSurfaceData( Time, Location, Speed );
% Set up fittype and options.
ft = fittype( 'loess' );
opts = fitoptions( 'Method', 'LowessFit' );
opts.Normalize = 'on';
opts.Span = 0.05;
% Fit model to data.
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
% Make contour plot.
figure( 'Name', 'Loess Quadratic' );
h = plot( fitresult, [xData, yData], zData, 'Style', 'Contour');
set(h(2),'MarkerSize',0.0001);
legend(h, 'Loess Quadratic', 'Speed vs. Time, Location', 'Location', 'NorthEast' );
% Label axes
xlabel Time(min)
ylabel Location(in)
grid on
h = colorbar
Thank you in advance for the help.

Accepted Answer

Unai San Miguel
Unai San Miguel on 28 Apr 2017
If, in the auto-generated code from the gui you specify h (the handler to the plots) as an aditional output variable you can access those data.
function [fitresult, gof, h] = createFit(x, y, z)
%CREATEFIT(X,Y,Z)
% Create a fit.
%
% Data for 'untitled fit 1' fit:
% X Input : x
% Y Input : y
% Z Output: z
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 28-Apr-2017 12:52:15
%%Fit: 'untitled fit 1'.
[xData, yData, zData] = prepareSurfaceData( x, y, z );
% Set up fittype and options.
ft = fittype( 'lowess' );
% Fit model to data.
[fitresult, gof] = fit( [xData, yData], zData, ft, 'Normalize', 'on' );
% Make contour plot.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, [xData, yData], zData, 'Style', 'Contour' );
legend( h, 'untitled fit 1', 'z vs. x, y', 'Location', 'NorthEast' );
% Label axes
xlabel x
ylabel y
grid on
h is an array and you have to guess which component is the one for the contour plot. In my example it was h(1). That component is the same as in [C, h] = contour(...), and you can use it in the same way. h(1).ContourMatrix has the matrix C that contour would output, and, in particular you can write
clabel(h(1).ContourMatrix, h(1))
to have the contour lines of your plot labelled.
%

More Answers (0)

Community Treasure Hunt

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

Start Hunting!