How to automating plot axis labels?

3 vues (au cours des 30 derniers jours)
Carl Mappas
Carl Mappas le 25 Mar 2020
Commenté : Carl Mappas le 25 Mar 2020
Hi all,
I have been using matlab's curve fitter app to fit an exponential curve to the number of active cases at a certain day. The function was generated with matlab's code generator and does this just fine.
I would like to automate how the labels are assigned on the Explonential Curve subplot and the Residuals Subplot so on each subplot the x and y labels are assigned from the createFit(day, cases) input variables.
So if I wanted to fit a curve to the number of new cases per day (new_cases), i would call createFit(day,new_cases) the x and y label of each subplot would be the name of the respective input variable, day and new_cases, rather then changing the label inside the function.
function [fitresult, gof] = createFit(day, cases)
[xData, yData] = prepareCurveData( day, cases );
ft = fittype( 'exp1' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [28.0415018660604 0.202270403686444];
[fitresult, gof] = fit( xData, yData, ft, opts );
figure( 'Name', 'Exponential_Curve_Corona' );
subplot( 2, 1, 1 );
h = plot( fitresult, xData, yData );
legend( h, 'New Cases vs. day', 'Exponential_Curve_Corona', 'Location', 'Best', 'Interpreter', 'none' );
xlabel('day', 'Interpreter', 'none' );
ylabel('cases', 'Interpreter', 'none' );
xlim([min(xData) max(xData)])
grid on
subplot( 2, 1, 2 );
h = plot( fitresult, xData, yData, 'residuals' );
legend( h, 'Exponential_Curve_Corona - residuals', 'Zero Line', 'Location', 'Best', 'Interpreter', 'none' );
xlabel('day', 'Interpreter', 'none' );
ylabel('cases', 'Interpreter', 'none' );
xlim([min(xData) (max(xData)+0.1)])
grid on
end
Thank you in advance for any suggestions :)

Réponse acceptée

Subhamoy Saha
Subhamoy Saha le 25 Mar 2020
Use the following lines.
xlabel(['day-', inputname(1)], 'Interpreter', 'none' );
ylabel(['cases-', inputname(2)], 'Interpreter', 'none' );
Note that inputname(1) or inputname(2) will be empty if the input was an expression.
  1 commentaire
Carl Mappas
Carl Mappas le 25 Mar 2020
Thank you Subhamoy it works perfectly

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interpolation dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by