Assign XLabel, YLabel from variable, fix YLim

13 views (last 30 days)
matthias82 on 19 Mar 2018
Edited: Julian Hapke on 19 Mar 2018
Hi, I've got problems with the figure, plot and axes functions and handles.
Some variables already exist in the workspace prior to this script which I import and I want to plot. Var1, Var2: type char x1 and y1 are the data matrices anyways.
Here's the code first:
h_fig1 = figure(1);
h_fig1.Name = [Var1, ' vs. ',Var2];
h_ax1 = axes;
h_ax1.YLimMode = 'manual';
h_ax1.YLim = [50,60];
h_ax1.YLimSpec = 'tight';
h_ax1.XLabel = Var1;
h_ax1.YLabel = Var2;
h_plot1 = plot(h_ax1,x1,y1);
What I desire to do: 1. Label the axes according to the CHAR variables above. I tried to convert to string before - didn't work either. Whatever, MatLAB returns the following error:
"While setting property 'XLabel' of class 'Axes':
Value must be ''.
Error in Plot_fct(line 17)
h_ax1.XLabel = Var1;"
2. Set the limit of Y fixed from 50 to 60. Yet when I append the plot function afterwards and the plot function finds data below or above the limit, it seems to ignore the limits and spread the YLim above the entire range. How can I really fix the Axis limits? I tried to append "tight", but that didn't help either.
Thanks, Matts

Answers (1)

Julian Hapke
Julian Hapke on 19 Mar 2018
Edited: Julian Hapke on 19 Mar 2018
doc xlabel
Edit: extented explanation: ax_handle.XLabel returns a handle for the label. This object has a property named "String", which can be used to change the displayed string.
For the second Question: As long as the ax_handle.YLimMode == 'manual', the plot should not change when
hold on
was set. If you just plot into the same axis again, it is recalculated and the properties are reset, if you set
ax_handle.NextPlot = 'replacechildren'
the limits are maintained.


Community Treasure Hunt

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

Start Hunting!