How to automating plot axis labels?
조회 수: 3 (최근 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 :)
댓글 수: 0
채택된 답변
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 Center 및 File Exchange에서 Interpolation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!