Matlab txt内容替换函数 fgetl fseek
2014-04-18 23:04
176 查看
Data Import and Export :Low-Level File I/O
the contents of the file:
16 5 9 4
2 11 7 14
3 10 6 15
13 8 12 1
55 55 55 55
Example — Overwriting an Existing Text File. Replace the third line of the file changing.txt from the previous example with [33 33 33 33]:
将第三行换成33 33 33 33
replaceLine = 3;
myformat = '%5d %5d %5d %5d\n';
% Open the file with permission to read and update.
% Use fgetl, which reads a line at a time,
% to place the file position indicator at the third line.
fid = fopen('changing.txt','r+');
for k=1:(replaceLine-1);
fgetl(fid);
end;
%将指针移至第三行起始处
% call fseek between read and write operations
fseek(fid, 0, 'cof');
% 在当前位置开始写入
% print the new values
fprintf(fid, myformat, [33 33 33 33]);
%将第三行替换
% close the file
fclose(fid);
To view the file, use the type function:
type changing.txt
This command returns the new contents of the file:
16 5 9 4
2 11 7 14
33 33 33 33
13 8 12 1
55 55 55 55
>> help fseek
fseek Set file position indicator.
STATUS = fseek(FID, OFFSET, ORIGIN) repositions the file position
indicator in the file associated with the given FID. fseek sets the
position indicator to the byte with the specified OFFSET relative to
ORIGIN.
FID is an integer file identifier obtained from FOPEN.
OFFSET values are interpreted as follows:
>= 0 Move position indicator OFFSET bytes after ORIGIN.
< 0 Move position indicator OFFSET bytes before ORIGIN.
ORIGIN values are interpreted as follows:
'bof' or -1 Beginning of file
'cof' or 0 Current position in file
'eof' or 1 End of file
STATUS is 0 on success and -1 on failure. If an error occurs, use
FERROR to get more information.
>> help fgetl
fgetl Read line from file, discard newline character.
TLINE = fgetl(FID) returns the next line of a file associated with file
identifier FID as a MATLAB string. The line terminator is NOT
included. Use FGETS to get the next line with the line terminator
INCLUDED. If just an end-of-file is encountered, -1 is returned.
If an error occurs while reading from the file, fgetl returns an empty
string. Use FERROR to determine the nature of the error.
MATLAB reads characters using the encoding scheme associated with the
file. See FOPEN for more information.
fgetl is intended for use with files that contain newline characters.
Given a file with no newline characters, fgetl may take a long time to
execute.
Example
fid=fopen('fgetl.m');
while 1
tline = fgetl(fid);
if ~ischar(tline), break, end
disp(tline)
end
fclose(fid);
>> help ischar
ischar True for character array (string).
ischar(S) returns 1 if S is a character array and 0 otherwise.
源文档 <http://hi.baidu.com/curbzz/item/e3661599d4b3e48159146139>
the contents of the file:
16 5 9 4
2 11 7 14
3 10 6 15
13 8 12 1
55 55 55 55
Example — Overwriting an Existing Text File. Replace the third line of the file changing.txt from the previous example with [33 33 33 33]:
将第三行换成33 33 33 33
replaceLine = 3;
myformat = '%5d %5d %5d %5d\n';
% Open the file with permission to read and update.
% Use fgetl, which reads a line at a time,
% to place the file position indicator at the third line.
fid = fopen('changing.txt','r+');
for k=1:(replaceLine-1);
fgetl(fid);
end;
%将指针移至第三行起始处
% call fseek between read and write operations
fseek(fid, 0, 'cof');
% 在当前位置开始写入
% print the new values
fprintf(fid, myformat, [33 33 33 33]);
%将第三行替换
% close the file
fclose(fid);
To view the file, use the type function:
type changing.txt
This command returns the new contents of the file:
16 5 9 4
2 11 7 14
33 33 33 33
13 8 12 1
55 55 55 55
>> help fseek
fseek Set file position indicator.
STATUS = fseek(FID, OFFSET, ORIGIN) repositions the file position
indicator in the file associated with the given FID. fseek sets the
position indicator to the byte with the specified OFFSET relative to
ORIGIN.
FID is an integer file identifier obtained from FOPEN.
OFFSET values are interpreted as follows:
>= 0 Move position indicator OFFSET bytes after ORIGIN.
< 0 Move position indicator OFFSET bytes before ORIGIN.
ORIGIN values are interpreted as follows:
'bof' or -1 Beginning of file
'cof' or 0 Current position in file
'eof' or 1 End of file
STATUS is 0 on success and -1 on failure. If an error occurs, use
FERROR to get more information.
>> help fgetl
fgetl Read line from file, discard newline character.
TLINE = fgetl(FID) returns the next line of a file associated with file
identifier FID as a MATLAB string. The line terminator is NOT
included. Use FGETS to get the next line with the line terminator
INCLUDED. If just an end-of-file is encountered, -1 is returned.
If an error occurs while reading from the file, fgetl returns an empty
string. Use FERROR to determine the nature of the error.
MATLAB reads characters using the encoding scheme associated with the
file. See FOPEN for more information.
fgetl is intended for use with files that contain newline characters.
Given a file with no newline characters, fgetl may take a long time to
execute.
Example
fid=fopen('fgetl.m');
while 1
tline = fgetl(fid);
if ~ischar(tline), break, end
disp(tline)
end
fclose(fid);
>> help ischar
ischar True for character array (string).
ischar(S) returns 1 if S is a character array and 0 otherwise.
源文档 <http://hi.baidu.com/curbzz/item/e3661599d4b3e48159146139>
相关文章推荐
- C++用fscanf()函数读取TXT文件内容
- 替换deque中的内容:assign()成员函数
- matlab文件操作及读txt文件(fopen,fseek,fread,fclose)
- 利用matlab把指定字符串在原位置替换为新的字符串(替换txt文件中的部分数据)
- Matlab读取.txt文件内容
- MATLAB 根据条件修改TXT文本内容
- php读取外部txt文件内容并打印在页面|fopen()函数
- fseek(),ftell()文件内容定位函数
- mysql 字符串替换 REPLACE()函数 剔除内容中空格换行回车
- MATLAB文件操作及读txt文件(fopen,fseek,fread,fclose)
- 字段名称Asp.Net中替换JSON中主键内容的函数(很简单,示例+说明)
- Asp.Net中替换JSON中主键内容的函数(很简单,示例+说明)
- 第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好
- matlab读取txt文件里面的内容并绘图
- Matlab读txt的几种函数
- matlab读取内容为二进制的TXT文件
- 【Java】Txt 文件内容替换
- C++string中有关字符串内容修改和替换的函数浅析
- 将 DbGrid查询内容的导出为Txt函数——Delphi
- matlab 实践程序7——将以文本文件的内容分批保存到多个txt文本中