Matlab之文件读写
你可以将txt的一些文本数据直接拷贝到matlab窗口,然后保存为mat文件,下次就可以直接采用load函数了。
ASCII文件也称为文本文件,这种文件在磁盘中存放时每个字符对应一个字节,用于存放对应的ASCII码。例如,数5678的存储形式为:
ASC码:00110101(5) 00110110(6) 00110111(7) 00111000(8) 共占用4个字节。ASCII码文件可在屏幕上按字符显示, 例如源程序文件就是ASCII文件,用DOS命令TYPE可显示文件的内容。由于是按字符显示,因此能读懂文件内容。
二进制文件是按二进制的编码方式来存放文件的。例如,数5678的存储形式为:00010110 00101110 (十进制5678转换成二进制)只占二个字节。二进制文件虽然也可在屏幕上显示,但其内容无法读懂。C系统在处理这些文件时,并不区分类型,都看成是字符流,按字节进行处理。输入输出字符流的开始和结束只由程序控制而不受物理符号(如回车符)的控制。因此也把这种文件称作“流式文件”。
Start
0 1 2
1 2
1 2 3 4
textdata: {2x1 cell}
1 2 NaN
1 2 3
4 NaN NaN
'Start'
1 2
1 2 3 4
End.
1 2 NaN
1 2 3
4 NaN NaN
1 2 3
1 2 3
End.
1 2
1 2 3
End.
fid2=fopen('numbers.txt','w');
while ~feof(fid1)
aline=fgetl(fid1);
if double(aline(1))>=48&&double(aline(1))<=57
fprintf(fid2,'%s\n',aline);
continue
end
end
fclose(fid2);
还有另外的方法
在MATLAB中,来读取和写入文本文件是很简单的事。下面,就来简单介绍下。如果有其他问题,请留言。
一、读取文本文件
思路:
1、用fopen来打开一个文件句柄
2、用fgetl来获得文件中的一行,如果文件已经结束,fgetl会返回-1
3、用fclose来关闭文件句柄
比如,TIM_Grid_Data.txt的内容如下:
0.1 0.1 151.031 -12.3144 -29.0245 3.11285
0.1 0.2 120.232 -2.53284 -8.40095 3.3348
0.1 0.3 136.481 -0.33173 -22.4462 3.598
0.1 0.4 184.16 -18.2706 -54.0658 2.51696
0.1 0.5 140.445 -6.99704 -21.2255 2.4202
0.1 0.6 127.981 0.319132 -29.8315 3.11317
0.1 0.7 106.174 -0.398859 -39.5156 3.97438
0.1 0.8 105.867 -20.1589 -13.4927 11.6488
0.1 0.9 117.294 -11.8907 -25.5828 4.97191
0.1 1 79.457 -1.42722 -140.482 0.726493
0.1 1.1 94.2203 -2.31433 -11.9207 4.71119
那么可以用下面的代码来读取该文本文件:
fid=fopen('TIM_Grid_Data.txt','r');
best_data=[];
while 1
tline=fgetl(fid);
if ~ischar(tline),break;end
tline=str2num(tline);
best_data=[best_data;tline];
end
fclose(fid);
这样文本文件中的内容就读入到了best_data中了。
写文件
思路:
1、用fopen打开一个文件句柄,但要用“w+”或“r+”等修饰符,具体参看help fopen
|
|
Open file for reading. |
|
|
Open or create new file for writing. Discard existing contents, if any. |
|
|
Open or create new file for writing. Append data to the end of the file. |
|
|
Open file for reading and writing. |
|
|
Open or create new file for reading and writing. Discard existing contents, if any. |
|
|
Open or create new file for reading and writing. Append data to the end of the file. |
|
|
Open file for appending without automatic flushing of the current output buffer. |
|
|
Open file for writing without automatic flushing of the current output buffer. |
2、用fprintf写入数据
3、用fclose来关闭文件句柄
比如下面的程序:
fid=fopen('Data.txt','a+');
fprintf(fid,'Hello,Tim\r\n');
fprintf(fid,'http://blog.sina.com.cn/pengtim');
a=rand(1,10);
fprintf(fid,'%g\r\n',a);
fclose(fid);
打开Data.txt文件,可以看到:
Hello,Tim
http://blog.sina.com.cn/pengtim0.655741
0.0357117
0.849129
0.933993
0.678735
0.75774
0.743132
0.392227
0.655478
0.171187
写文件:
Matlab之文件读写的更多相关文章
- 通过文件读写方式实现Matlab和Modelsim的联合仿真
虽然Modelsim的功能非常强大,仿真的波形可以以多种形式进行显示,但是当涉及到数字信号处理的算法的仿真验证的时候,则显得有点不足.而进行数字信号处理是Matlab的强项,不但有大量的关于数字信号处 ...
- MATLAB中文件的读写和数据的导入导出
http://blog.163.com/tawney_daylily/blog/static/13614643620111117853933/ 在编写一个程序时,经常需要从外部读入数据,或者将程序运行 ...
- C++文件读写函数之——fopen、fread和fwrite、fgetc和fputc、fgets和fputs、ftellf和fseek、rewind
由于最近经常使用到c语言中的读写文件,所以在此总结以下,方便以后查找. 在c中,文件操作都是由库函数来实现的,主要是分为读和写两种操作,以下详细讲解以下所有有关文件操作的邯郸乎的用法: //C++写入 ...
- mat文件读写
一起来学演化计算-mat文件读写 觉得有用的话,欢迎一起讨论相互学习~Follow Me Matlab读取和保存mat文件数据 在matlab命令行中输入save 变量名a,将a变量保存在新生成的a. ...
- 【Win 10 应用开发】文件读写的三种方案
本文老周就跟伙伴们探讨一下关于文件读写的方法.总得来说嘛,有三种方案可以用,而且每种方案都各有特色,也说不上哪种较好.反正你得记住老祖宗留给我们的大智慧——事无定法,灵活运用者为上. OK,咱们开始吧 ...
- Matlab 读取文件夹中所有的bmp文件
将srcimg文件下的bmp文件转为jpg图像,存放在dstimg文件夹下 str = 'srcimg'; dst = 'dstimg'; file=dir([str,'\*.bmp']); :len ...
- c语言文件读写操作总结
C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...
- ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调
近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0,现把学习结果分享一下,希望对新手有帮助. 目录 ActionScript 3.0简介 Hello ...
- Android 文件读写
一.分类 文件读写作为Android四大数据存储方式之一,又分为内部存储和外部存储两种: (1)内部存储(Internal storage): 总是可用. 文件默认情况存储在/data/data/包名 ...
随机推荐
- MES总结:CBF.Common 文件Net下的有类型转换
MES总结:CBF.Common 文件Net下的有类型转换. using System.Text;using System.Data;using System.ComponentModel;using ...
- myeclipse中working Sets
最近myeclipse中的项目太多了,看起来老不爽,查找还不方便,发现这个working Sets还是挺好用的 接下来的步骤,太简单了有木有,就不写了 0.0
- Rstudio安装
1.https://www.r-project.org/下载R语言(注意32位还是46位系统). 2.安装R,尽量默认安装路径,安装路径不要有中文. 3.https://www.rstudio.com ...
- Ext_两种处理服务器端返回值的方式
1.Form表单提交返回值处理 //提交基本信息表单 f.form.submit({ clientValidation:true, //表单提交后台处理地址 url:' ...
- ARM architectures
https://gitorious.org/freebsd/freebsd/raw/56c5165837bf08f50ca4a08c6b2da91f73852960:sys/arm/include/a ...
- 备份服务器数据(IIS配置备份还原、任务计划、服务列表和APP)
该脚本可以用来导出IIS配置.任务计划.服务列表和APP,同时支持Windows 2003和2008. #定义备份位置 $iisfolder = "d:\Backup_all\IIS&quo ...
- hdu 5592 ZYB's Game 树状数组
ZYB's Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=55 ...
- NGUI ERROR:UnityException: Sprite is not rectangle-packed. TextureRect is invalid.解决
在使用Ngui 3.4.9的时候,使用“Unity 2D Sprite”控件的时候,出现了UnityException: Sprite is not rectangle-packed. Texture ...
- 【Animation】 使用handler和Runnable实现某一个控件的抖动效果
布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tool ...
- You don't have permission to access / on this server
配置虚拟主机的时候,里面加上如下: <Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOv ...