Print, fprint, fscanf, and disp functions of MATLAB

Source: Internet
Author: User

Print, fprint, fscanf, and disp functions of MATLAB


Print:

The print function can save the function image as an image:

 
Minbnd =-4 * PI; maxbnd = 4 * PI; t = minbnd: 0.1 * Pi: maxbnd; plot (T, sin (t), 'G', 'linewidth ', 2); line ([minbnd, maxbnd], [0, 0]); % draw the X axis ([-10, 10,-2, 2]) % defines the displayed coordinate range: X Between (-10, 10), y between (-2, 2) grid between '); % Save As PNG image, in the current working directory of MATLAB

As follows:


Open the work directory of Step 7 and you will be able to see the sin.png image.


Print ('-dpng', 'sin.png' must be saved as a PNG image, and the file name is sin.png. The first parameter can be:

-Dbmp: Save As BMP

-Djpeg: Save As JPEG

-Dpng: Save As PNG

-Dpcx: Save As PCX format

-Dpdf: Save as PDF

-Dtiff: Save As tiff




Fprintf:

The fprintf function can write data to a text file in the specified format:

 
Data = [5, 1, 2; 3, 7, 4]; [row, Col] = size (data); for I = 1: Row for j = 1: col fprintf ('data (% d, % d) = % d \ n', I, j, data (I, j); % output directly to the screen; similar to the C language output format endend


FID in fprintf (FID, format, data) indicates the file handle opened by the fopen function. If FID is omitted, it is directly output to the screen. format is the output format in string format, data is the data to be output. The format can be:

% C single character % d signed decimal number (% I can also) % u unsigned decimal number % F floating point number (% 8.4f indicates taking the 8-Bit Width of the floating point number, at the same time, 4 decimal places) % O unsigned octal digits % s string % x lowercase a-f hexadecimal digits % x size a-f hexadecimal digits


Output to file:

 
Data = [5, 1, 2; 3, 7, 4]; [row, Col] = size (data ); % calculate the number of rows and columns of matrix data % add t to output line breaks in Windows format, that is, 0xod 0x0a, without Tsung output line breaks in linuxformat, I .e. 0x0afid1_fopen('test.txt ', 'wt'); % open the file for I = 1: row for j = 1: Col fprintf (FID, '% d', data (I, j); % similar to the output format of end fprintf (FID, '\ n'); endfprintf (FID, 'This is a string \ n'); fprintf (FID,' % x', hex2dec ('abcd ')); fclose (FID); % do not forget to close the file!

The test.txt file will be generated in the work directory of the previous project named "Matlab ".


Read from file:

We can use the fscanf function.



Fscanf:

 
% The reason for adding T is the same as above: FID = fopen ('d: \ test.txt ', 'rt'); % read data to data. Where data is a matrix of 2*3 data = fscanf (FID, '% d', [2, 3]); s = fscanf (FID,' % s '); D = fscanf (FID, '% x'); % close the file fclose (FID); disp (data); disp (s); disp (d );

From the output results, we find thatSpaces are ignored when fscanf reads data until you press Enter!



Disp:

The DISP function directly outputs the content in the MATLAB command window:

 
% Single string output: disp ('Hello world! '); % Output of different data types: num1 = 1; num2 = 2; disp ([num2str (num1),' + ', num2str (num2),' = ', num2str (num1 + num2)]);

Output:

Hello world!

1 + 2 = 3



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.