How to automating plot axis labels?

7 次查看(过去 30 天)
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 :)

采纳的回答

Subhamoy Saha
Subhamoy Saha 2020-3-25
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by