MATLAB Answers

Adding text to axis label.

9 views (last 30 days)
Ronald Bednarczyk
Ronald Bednarczyk on 15 Jan 2020
Answered: Ronald Bednarczyk on 16 Jan 2020
I have several codes that read in data from Excel files based on parameter mneumonics. Those mneumonics are used to search another Excel file for a more descriptive parameter name. For example mneumonic ADCIAS_PCM2 will retieve INDICATED AIRSPEED (KTS) as a plot axis label. That axis label is universal and I use it for my x-axis. I want to add text at each end of the x-axis for REARWARD (at the negative x-axis end) and FORWARD (at the positive x-axis end). The current plot goes from -50 to +50. I used the following lines and they work, but the problem arises if the y-axis scaling is different the text will be higher or lower relative to the x-axis. How can I add this text such that it will stay inline with the xlabel?
text(-50,-0.1,'RWD','FontSize',8,'FontWeight','bold');
text( 47,-0.1,'FWD','FontSize',8,'FontWeight','bold');

  0 Comments

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 15 Jan 2020
Use the xlabel() and ylabel() commands instead.
xlabel('RWD','FontSize',8, 'FontWeight','bold');
If you want to use text() because you want the text to be in some box somewhere on the graph, then you'll have to replace those numbers for x and y in text() with something based on xlim() and ylim(). For example
% Place text 40% of the way over in x and 60% up in y.
caption = sprintf('The direction is %s', directionString);
xl = xlim;
yl = ylim;
xt = xl(1) + 0.4 * (xl(2) - xl(1));
yt = yl(1) + 0.6 * (yl(2) - yl(1));
text(xt, yt, caption,'FontSize',8, 'FontWeight','bold', 'Horizontal Alignment', 'left');

  0 Comments

Sign in to comment.


Ronald Bednarczyk
Ronald Bednarczyk on 16 Jan 2020
I did this instead. The maxy just sets the ylim to the next highest decimal.
xlabel(X_label_txt);
ylabel(cell2mat(Y_axis_label_cell(param_no)));
legend('show','Location','northwest');
xlim([-50 50]); %Fixed X-axis scale
if max(ylim)>=1
maxy=ceil(max(ylim));
elseif max(ylim)*10>=1
maxy=ceil(max(ylim)*10)/10;
elseif max(ylim)*100>=1
maxy=ceil(max(ylim)*100)/100;
end
set(gca, 'YLim', [0, maxy]);
grid on;
%Set X and Y-axis tick mark & label font type & size
ax = gca;
ax.YAxis.TickLabelFormat = '%,.2f';
ax.FontSize = 8;
ax.FontName = 'Courier';
ax.FontWeight = 'bold';
ax.GridColor = [0, 0, 0];
ax.GridLineStyle = ':';
ax.GridAlpha = 0.8;
ytext=-0.2*maxy;
text(-50,ytext,'LSF','FontSize',8,'FontWeight','bold');
text( 47,ytext,'RSF','FontSize',8,'FontWeight','bold');

  0 Comments

Sign in to comment.

Sign in to answer this question.