How MATLAB transforms the matrix data into TXT file save _matlab

Source: Internet
Author: User
Turn from: http://blog.csdn.net/lsg32/article/details/8618161
The specific command is: with Save *.txt-ascii x x as the variable *.txt file name, which is stored in the current working directory, and then open to open, the data may be stored in an exponential manner.

Look at the following example:

(a) =[17 1 8 15;23 5 7, 4 6, 3 2]; save afile.txt-ascii A.
After the Afile.txt is opened, it is like this: 1.7000000e+001 2.4000000e+001 1.0000000e+000 8.0000000e+000 1.5000000e+001 2.3000000e+001 5.0000000e+000 7.0000000e+000 1.4000000e+001 1.6000000e+001 4.0000000e+000 6.0000000e+000 1.3000000e+001 2.0000000e+ 001 2.2000000e+001 1.0000000e+001 1.2000000e+001 1.9000000e+001 2.1000000e+001 3.0000000e+000 1.1000000e+001 1.8000000e+001 2.5000000e+001 2.0000000e+000 9.0000000e+000

There are many similar problems on the web, but none of them are ideal, and here's a way to solve the problem: using the fprintf command: Take the example above:

In the first case:

>> a=[17 24 1 8 15;23 5 7 14 16; 4 6 13 20 22; 10 12 19 21 3; 11 18 25 2-9]; >> FID = fopen (' B.txt ', ' W ');       fprintf (FID, '%gn ', a); # N-line fclose (FID);

Then use WordPad to open b.txt, which reads as follows: for column vectors

17 23 4 10 11 24 5 6 12 18 1 7 13 19 25 8 14 20 21 2 15 16 22 3-9

In the second case:

Make changes to the above command: # N line change to t,table key

>> FID = fopen (' B.txt ', ' W '); fprintf (FID, '%gt ', a); Fclose (FID);

Then use WordPad to open the B.txt, which reads as follows: for the row vector:


17 23 4 10 11 24 5 6 12 18 1 7 13 19 25 8 14 20 21 2 15 16 22 3-9

In the third case: combining the above two results, we write the following command:

Fid=fopen (' B.txt ', ' w ');% write to file path [M,n]=size (a);       For i=1:1:m for J=1:1:n if J==n fprintf (FID, '%gn ', A (i,j));        else fprintf (FID, '%gt ', A (i,j)); End-end fclose (FID);

Then use WordPad to open the B.txt, which reads as follows: Matrix

17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2-9
Description: All of the above are done in the current working directory. Below gives the most general model, we can try to operate on their own, if you need dat format, directly to the TXT for DAT can, thanks to my classmate Jiang Yongji.                                                Fid=fopen (' c:documents and settingscleantotal.ped ', ' wt ');% write to file path Matrix=input_mattrix %input_matrix to Output matrix
[M,n]=size (Matrix);
For i=1:1:m
For J=1:1:n
If J==n
fprintf (FID, '%g\n ', Matrix (i,j));
Else
fprintf (FID, '%g\t ', Matrix (i,j));
End
End
End
Fclose (FID);

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.