Matlab txt内容替换函数 fgetl fseek
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>
Matlab txt内容替换函数 fgetl fseek的更多相关文章
- Oracle内置函数:时间函数,转换函数,字符串函数,数值函数,替换函数
dual单行单列的隐藏表,看不见 但是可以用,经常用来调内置函数.不用新建表 时间函数 sysdate 系统当前时间 add_months 作用:对日期的月份进行加减 写法:add_months(日期 ...
- php使用file函数、fseek函数读取大文件效率分析
php读取大文件可以使用file函数和fseek函数,但是二者之间效率可能存在差异,本文章向大家介绍php file函数与fseek函数实现大文件读取效率对比分析,需要的朋友可以参考一下. 1. 直接 ...
- php中替换函数主要用的几个函数strtr(),str_repalce()。
php中替换函数主要有strtr(),str_repalce()这两个函数,今天介绍下他们的区别和用法, 先来看看这个php字符串替换函数 strtr()的两种用法: strtr(string,fro ...
- python字符串内容替换的方法(转载)
python字符串内容替换的方法 时间:2016-03-10 06:30:46来源:网络 导读:python字符串内容替换的方法,包括单个字符替换,使用re正则匹配进行字符串模式查找与替换的方法. ...
- JavaScript字符串插入、删除、替换函数
JavaScript字符串插入.删除.替换函数 说明: 以下函数中前两个函数取出查找字符串的前一部分和后一部分,以用于其他函数.注意,调用一次 replaceString(mainStr,search ...
- Oracle的字符替换函数translate用法
参考文档如下:http://www.banping.com/2009/05/18/oracle_function_translate/ Oracle提供了一个字符替换函数translate,不同于re ...
- Matlab调用C语言函数
Matlab调用C语言函数 如果我有一个用C语言写的函数,实现了一个功能,如一个简单的函数:double add(double x, double y) { return x + y ;}现在我想要在 ...
- mysql :SQL语句中的替换函数replace
replace() 用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式. 语法 REPLACE ( 'string_expression1' , 'string_expressio ...
- sql中保留一位小数的百分比字符串拼接,替换函数,换行符使用
select num ,cast(round(convert(float,isnull((a.Sum_Num-d.Sum_Num),0))/convert(float,c.Sum_Store_Num ...
随机推荐
- 为什么Erlang比C慢那么多倍?
Erlang 一直以慢“著称”,本文就来看看 Erlang 慢在什么地方,为什么比实现同样功能的 C 语言程序慢那么多倍.Erlang 作为一种虚拟机解释的语言,慢是当然的.不过本文从细节上分析为什么 ...
- Effective Java 47 Know and use the libraries
Advantages of use the libraries By using a standard library, you take advantage of the knowledge of ...
- 通过正则获取URL中的参数
闲着无聊用正则做了一个获取URL参数的小算法^_^ function getParam(name) { var objs = window.location.search.match("(\ ...
- 关于Redis的启动过程
一.简介 Redis的启动也就是main函数的执行,程序的入口在redis.c中,启动流程: 1. 初始化默认服务器配置,如果是sentinel模式还需进行额外的配置 2. 修改配置文件或配置选项,这 ...
- linux下批量修改存有超大数据量IP文件中的IP内容以及去重排序
作为一个linux的学徒,分享一下自己解决这个小问题的心得,在处理这个问题时使用了一个小技巧感觉很适用,个人发觉linux的终端真滴是非常强大,下面就详细地介绍这个问题以及解决办法吧 问题描述:由于要 ...
- fork函数
在Unix/Linux中用fork函数创建一个新的进程.进程是由当前已有进程调用fork函数创建,分叉的进程叫子进程,创建者叫父进程.该函数的特点是调用一次,返回两次,一次是在父进程,一次是在子进程. ...
- Windows 系统下json 格式的日志文件发送到elasticsearch
Windows 系统下json 格式的日志文件发送到elasticsearch配置 Nxlog-->logstash-->ElasticSearch Logstash https://ww ...
- PHPDBG
一.简介 PHPDBG的是一个轻量级.强大.易用的PHP调试平台.可以在PHP5.4和之上版本中使用.在php5.6和之上版本将内部集成. 二.安装 PHP源码下载 http://php.net/gi ...
- [转]Getting Start With Node.JS Tools For Visual Studio
本文转自:http://www.c-sharpcorner.com/UploadFile/g_arora/getting-started-with-node-js-tools-for-visual-s ...
- 读高性能JavaScript编程学英语 第一章第三页第一段话
When the browser encounters a <script> tag, as in this HTML page, there is no way of knowing w ...