MATLAB Answers

How to automating plot axis labels?

4 ビュー (過去 30 日間)
Carl Mappas
Carl Mappas 2020 年 3 月 25 日
コメント済み: Carl Mappas 2020 年 3 月 25 日
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.
  1 件のコメント
Carl Mappas
Carl Mappas 2020 年 3 月 25 日
Thank you Subhamoy it works perfectly

サインインしてコメントする。

その他の回答 (0 件)

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by