The function disp
One way to display data is by using the disp function. The disp function takes an array as an argument and displays the values of the array in the command window. If the type of the array is char, it displays the string contained in the array.
The disp function is often used in conjunction with the num2str function (which converts numbers to strings) and int2str (which converts integers to strings) to create messages to be displayed in the command window.
>> str = ['The value of pi = ' num2str(pi)]
str =
'The value of pi = 3.1416'
>> disp(str)The value of pi = 3.1416
Using the fprintf function for formatted output
A more flexible way to display data is by using the fprintf function. The fprintf function can display one or more values along with related text and allows the programmer to control how the values are displayed. The general form is:
fprintf(format, data)
Where format is a string that describes how the data should be displayed, containing the text to be displayed and special characters that describe the data format. For example:
>> fprintf('The value of pi is %f \n',pi)
The value of pi is 3.141593
Where %f is called a conversion character, indicating that the value in the data list should be displayed in floating-point format at that position in the string. The character \n is an escape character that indicates a newline should be output so that the text below starts on a new line. Some common conversion characters are shown in the table below:
| Format String | Function |
|---|---|
| %d | Displays the value in integer format |
| %e | Displays the value in exponential format |
| %f | Displays the value in floating-point format |
| %g | Displays the value in the shorter of floating-point or exponential format |
| \n | New line |
It is also possible to specify the field width and the number of decimal places to display. This is done by specifying the width and precision after the % and before the f. For example:
>> fprintf('The value of pi is %6.2f \n', pi)The value of pi is 3.14
Where %6.2f indicates that the output should display the number in floating-point format with a field width of 6, rounded to two decimal places.
The fprintf function has a significant limitation: it only displays the real part of a complex number. When calculations yield complex results, this limitation can lead to incorrect results. In such cases, it is better to use the disp function to display the answer.
>> x = 2 * (1 - 2 * i)^3;
>> str = ['disp: x = ' num2str(x)];
>> disp(str);disp: x = -22+4i
>> fprintf('fprintf: x = %8.4f \n',x)fprintf: x = -22.0000
How to save and load data files?
There are many ways to load and save data files in MATLAB. Here, we will only introduce the simplest methods, which are the load and save commands.
The save command saves data from the current MATLAB workspace to a disk file. The common form of this command is:
save filename var1 var2 var3
Where filename is the name of the file to save the variables, and var1, var2, etc. are the variables to be saved in the file. By default, the filename will be given the extension mat, and such data files are called MAT files. If no variables are specified, the entire workspace will be saved.
If data must be exchanged between MATLAB and other programs, save MATLAB data in ASCII format. If the data is used only within MATLAB, save it in MAT file format.
my_data = [1.23 2 3; 4 5 6];
>> save my_data.dat my_data -ascii
Use the dat extension to save ASCII data files to distinguish them from mat extension MAT files.
The load command is the opposite of the save command. It loads data from a disk file into the current MATLAB workspace. The common form of this command is:
load filename
Where filename is the name of the file to load.
Loading a MAT file
load filename
Loading a dat file
load filename.dat
Happy learning, may you only have joy and no worries~~