and in matlab fopen fprintf function summary

In the example in matlab function fopen to open the file specified as follows:

* 1) "fopen" to open the file, give the file code.

语法1:FID= FOPEN(filename,permission)

Open the file in the specified manner

FID = + N (N is a positive integer): indicates the file is opened successfully, the file code is N.

FID = -1: indicates the file open unsuccessful.

FID before the file is closed always effective.

If you open for reading, matlab first searches the working directory, followed by a search of other directories matlab, "permission" is to open the way parameters.

Open string determined by the following parameters:

r read

w write (if the file exists, created automatically)

a subsequent write (if the file exists, automatically created)

r + reading and writing (file should already exist)

w + refresh writing (if the file exists, created automatically)

a + a subsequent write (if the file exists, automatically created))

w rewritten, but does not automatically refresh

a subsequent write, but does not automatically refreshed

 

File storage format: the default mode is to open the file: Binary. Open text, may be added to "t" file, such as "rt" in the mode parameter "permission" in, "wt +"

 

Specific use in matlab fprintf function examples are as follows:

fprintf function data can be written to a text file in the format specified. Its call format is:

Formatted output data: fprintf (fid, format, variables)

  According to the specified format value of the variable output to the screen or the specified file

  fid is a file handle, if the default, then output to the screen

    1 for standard output (the screen) or 2 for standard error. If FID is omitted, output goes to the screen.

  format format in which the data used to specify the output

    % d Integer

    % e real number: SCIENCE calculation form

    %f 实数:小数形式

    %g 由系统自动选取上述两种格式之一

    %s 输出字符串

fprintf(fid,format,A)

说明:fid为文件句柄,指定要写入数据的文件,format是用来控制所写数据格式的格式符,与fscanf函数相同,A是用来存放数据的矩阵。

例6.9 创建一个字符矩阵并存入磁盘,再读出赋值给另一个矩阵。

>> a='string';

>> fid=fopen('d:\char1.txt','w');

>> fprintf(fid,'%s',a);

>> fclose(fid);

>> fid1=fopen('d:\char1.txt','rt');

>> b=fscanf(fid1,'%s')

b =

string

matlab读txt文件

fid=fopen('fx.txt','r');

%得到文件号

[f,count]=fscanf(fid,'%f %f',[12,90]);

%把文件号1的数据读到f中。其中f是[12 90]的矩阵

%这里'%f %f'表示读取数据的形势,他是按原始数据型读出

fclose(fid);

%关闭文件

另外有的txt文件还可以用load来打开

其语句为

f=load('fx.txt)

 

源文档 <http://blog.sina.com.cn/s/blog_5e1cdcaf0100vmnf.html>

转载于:https://www.cnblogs.com/AI-Algorithms/p/3670867.html

Guess you like

Origin blog.csdn.net/weixin_33674976/article/details/94506300