print,fprint,fscanf,disp function of matlab (GO)

Source: Internet
Author: User

Print :

The print function can save a function graphic as a picture:

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 X axis

Axis ([-10, 10,-2, 2])% defines the coordinate interval shown: x between ( -10,10), y between ( -2,2)

Grid on;

Title (' Sin (x) ');

Xlabel (' x ');

Ylabel (' sin (x) ');

Print ('-dpng ', ' sin.png '); % saved as PNG image, under MATLAB current working directory

As follows:



Open the current working directory of MATLAB can see the sin.png picture



print ('-dpng ', ' sin.png ') represents a Save as PNG picture, file name is Sin.png , where the first parameter can be:

-dbmp : Save As BMP format

-djpeg : Save As JPEG format

-dpng : Save As PNG format

-dpcx : Save As PCX format

-dpdf : Save As PDF format

-dtiff : Save As TIFF format







fprintf :

fprintf function to 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)); % direct output to screen; output format similar to C language

End

End


fprintf (FID, format, data) in the FID expressed by fopen function to open the file handle, if FID omitted, the output is directly on the screen, format is the output format in the form of a string, Data is the data to be exported. where format can be:

%c single character

%d signed decimal number (%i can also)

%u unsigned decimal number

%f floating-point number (%8.4f = 8-bit width for floating-point numbers, 4 decimal places)

%o unsigned octal number

%s string

The hexadecimal number of the%x lowercase a-f

The hexadecimal number of the%x size a-f




Output to File:

data = [5, 1, 2; 3, 7,4];

[Row, col] =size (data); % to find the number of rows and columns of the matrix data

% plus t means output line breaks in Windows format, that is, 0xOD 0x0A, no t means output line breaks in linux format, i.e. 0x0A

Fid=fopen (' test.txt ', ' wt '); % Open File

For I=1:row

For J=1:col

fprintf (FID, '%d ', data (i, j)); % similar to the output format of the C language

End

fprintf (FID, ' \ n ');

End

fprintf (FID, ' This ISA string\n ');

fprintf (FID, '%x ', Hex2dec (' ABCD '));

Fclose (FID); % finally don't forget to close the file!

The Test.txt file will be generated in the current working directory of MATLAB



Read from File:

we can use fscanf function





fscanf :

% plus T for the same reason as above

Fid=fopen (' d:\test.txt ', ' RT ');

% to read the data. Where data is the matrix of 2*3

DATA=FSCANF (FID, '%d ', [2, 3]);

S=FSCANF (FID, '%s ');

D=FSCANF (FID, '%x ');

% Close File

Fclose (FID);

DISP (data);

Disp (s);

Disp (d);

From the results of the output, we found that fscanf Read the data will ignore the space, until the return!




disp :

disp function to output content directly in the Matlab in the command window:

% single-string output:

Disp (' Hello world! ');

% different types of data output:

NUM1 = 1;

num2 = 2;

DISP ([Num2str (NUM1), ' + ', Num2str (num2), ' = ', Num2str (num1+num2)]);

Output:

Hello world!

1 + 2 = 3

print,fprint,fscanf,disp function of matlab (GO)

Related Article

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.