Some useful plot tuning tools in MATLAB

Recently, once again checking the homework of my students, I have a burning desire to automate this process. The task was to compile a work table for deviating the magnetic compass and constructing a deviation curve.

The input data were the readings of the magnetic compass (MK), the synchronously observed readings of the gyrocompass (GK), the correction of the GK, and the magnetic declination value for the region in which the measurements were taken.

All data was entered into a table and separated from 10 columns with input data and 25 rows - input data values ​​for each of the options. For ease of reading data in MATLAB, they were written as a text file and imported into the workspace using the importdata function.

According to the calculation method, it was necessary to process the data using several empirical formulas to fill out the working table of the MK deviation. However, the main and most obvious result of the work is the construction of the deviation curve of MK.

To plot, the plot function was selected, which has a large number of settings that allow you to get the result in the right form. The code was compiled:

%Построение графика Рабочей кривой девиации
X=[0:10:360];
Y=SC;
plot(X,Y,'bo-');
grid on;
title('Рабочая кривая девиации');
xlabel('Компасный курс, \circ');
ylabel('Девиация, \circ');
axis auto
xlim([0,360])
set(gca, 'XTick',0:45:360)

And the following graph is received: Let's analyze the code line by line, consider what parameters you can specify to configure the display of graphs.





X=[0:10:360];
Y=SC;

Input data for plotting is set here. The number of values ​​along the abscissa and the ordinate should be the same. According to this data are vectors with 36 values.

plot(X,Y,'bo-');

Actually, the function of constructing a graph into which data and parameters are transmitted. In addition to the obvious input, the parameter of the function is the type of line displayed, encoded in a three-character combination. In this case, “b” is blue, the color of the line; “O” - type of marker, which indicates the points of the graph and “-” - type of line, in this case - solid.

Below is a list of parameters for setting the displayed line.

Marker Line color
c blue
m violet
y yellow
r red
g green
b blue
w white
k black

Marker Line type
- continuous
- - dashed
: dotted
-. dash-dotted

Marker Type of marker
. point
+ plus
sign * star sign
about circle
x cross sign

grid on;

The command to turn on the grid on the chart.

title('Рабочая кривая девиации');
xlabel('Компасный курс, \circ');
ylabel('Девиация, \circ');

Labels for the graph and corresponding axes. Here “\ circ” is the character encoding of the degree symbol.

axis auto

Axis control team. In this case, the “auto" parameter is set - automatic axis alignment. Here, MATLAB did not work for me, because automatically, the axes did not dock to the extreme values ​​of the graph, but “added” extra space along the “X” axis. Using the “help axis” command, I found several more options for the parameter for the axes, in particular, I tried the “tight” parameter, which was supposed to dock the boundaries of the graph to the extreme values ​​of the curve. However, the result of this parameter did not satisfy me either. the result was as follows: The graph looks “clamped”, in addition, parts of the curve between the maximum values ​​are “lost”. To get a clear result, we had to configure the “X” axis separately using the following commands:











axis auto
xlim([0,360])

The last function sets the boundary values ​​separately for the “X” axis, which allowed me to limit the graph to the maximum values ​​on this axis.

And the last command:

set(gca, 'XTick',0:45:360)

Allowed to configure captions and pitch for the “X” axis. The “set” function is quite general, its operation depends on the parameters passed. In this case, “gca” - means that the parameters will be set for the chart grid, “XTick” - means that the signature of the “X” axis will be controlled, and the parameter “0: 45: 360” - sets the minimum value, step and maximum value .

The result was a fairly clear graph of the deviation curve, by comparing the form of which with the form of the graph received by the student, it was possible to quickly assess the correctness of the work. Also, thanks to downloading data from a file for all options, with a further choice of the latter, only the option number had to be changed to get the result.



I hope that this article will be useful not only for MATLAB beginners, but also for experienced users.

In the end, I would like to note the usefulness of the “help” command - it not only allows you to get the necessary information on a function or command from the command line, but also does it much faster than searching in MATLAB help.

Also popular now: