explain ytick in matlab

In MATLAB, when we create a plot using the plot function, we can specify the y-axis limits and the position of the y-tick labels using the ytick property.

The ytick property accepts an array that specifies the y-tick values. For example, the following code sets the y-tick values to be -1, 0, 1, and 2:

main.m
% generate data
x = linspace(0, 2*pi);
y = sin(x);

% create plot and set yticks
plot(x, y);
yticks([-1 0 1 2]);
113 chars
8 lines

This code will create a sine wave and set the y-tick labels to be -1, 0, 1, and 2.

Alternatively, you can also set the y-tick values and labels using the yticklabels property. The following code sets the y-tick values to be -1, 0, 1, and 2, and the y-tick labels to be 'neg', 'zero', 'pos', and 'two':

main.m
% generate data
x = linspace(0, 2*pi);
y = sin(x);

% create plot and set yticks
plot(x, y);
yticks([-1 0 1 2]);
yticklabels({'neg', 'zero', 'pos', 'two'});
157 chars
9 lines

This code will create a sine wave and set the y-tick labels to be 'neg', 'zero', 'pos', and 'two'.

related categories

gistlibby LogSnag