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 ...
随机推荐
- 五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT)
当Adobe.Microsoft.Sun等一系列巨头开始表现出对”开源”的青睐时,”开源”的时代即将到来!现今存在的开源协议很多,而经过Open Source Initiative组织通过批准的开源协 ...
- 邮件群发工具(C#版)
引言 经常会参与组织一些社区活动,涉及到和不同的人进行交流,微信当然是必须的,同样邮件也是一种不可或缺的方式. 一般群发的邮件不是很友好,如果是一对一的,收到邮件的人是不是会比较重视,而且还有他的名字 ...
- 手把手集成web端手写公式功能
何为手写公式,很简单,就是在网页上可以写出数学公式,并能够生成Latex格式的字符串.废话不多说,直接走正题. 一.首先大家可以先去官网了解一下myscript这个插件 官方网站:https://de ...
- 使用网站websequencediagrams在线画时序图
在线画时序图的网站:https://www.websequencediagrams.com/ 该网站提供拖拉图形和编写脚本代码2个方式来制作时序图,同时提供多种显示风格. 实例: 1.脚本代码: ti ...
- SQLAlchemy 做migration的时候 ValueError: too many values to unpack
我在做数据库迁移的时候,使用sqlalchemy,出现一个问题 Traceback (most recent call last): File "/Volumes/Spano/Dropbox ...
- 用java程序输出自己的姓名
代码部分: public class Hello { public static void main(String[] args) { System.out.println("$$$$$$$ ...
- zookeeper适用场景:配置文件同步
问题导读:1.本文三个角色之间是什么关系?2.三个角色的作用是什么?3.如何代码实现这三个角色的作用? 在 zookeeper适用场景:zookeeper解决了哪些问题有关于分布式集群配置文件同步问题 ...
- 【读书笔记《Android游戏编程之从零开始》】15.游戏开发基础(剪切区域)
剪切区域也称为可视区域,是由画布进行设置的:它指的是在画布上设置一块区域,当画布一旦设置了可视区域,那么除此区域外,绘制的任何内容都将看不到:可视区域可以是圆形.矩形等等. 画布提供了三种设置可视区域 ...
- 读《深入理解Java虚拟机》有感——第一部分:Class文件的结构
1.产生 源码(.java文件)——>编译器(如:javac)——>字节码(.class文件)——>虚拟机(如:HotSpot)执行 2.Class文件 1)构成: ...
- codeforces 711D D. Directed Roads(dfs)
题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...