Matlab:如何读取CSV文件以及如何读取带有字符串数据项的CSV文件
2013-11-15 09:44
573 查看
CSV,逗号分开的文件,如果能快速的读取这些文件中的数据,无疑会帮助我们解决很多问题。
1、 只有数据的CSV文件,CSV file that includes only numbers.
As an example, create a text file, named as 'data.csv' if you prefer, which includes the following data with any editor you like.
1) Read all the data into a 3X4 matrix.
2) Read part of the data with specification of the start index. What is important is the data is accessed from index 0 in the direction of row and column.
Result showing below.
3) Read only the specified range.
Note the starting index you specified in the second and third parameters is the same with the first two arguments in the fourth parameter matrix.
2、 How to read a CSV file containing string data items.
Create a file, named as 'datastr.csv' if you like. Add the following data.
One who try to use csvread shall encounter frustration. :(
I propose another solution to solve this case.
The result showing below.
Try it and good luck. :)
1、 只有数据的CSV文件,CSV file that includes only numbers.
As an example, create a text file, named as 'data.csv' if you prefer, which includes the following data with any editor you like.
1, 2, 3, 4 5, 6, 7, 8 9, 0, 1, 2
1) Read all the data into a 3X4 matrix.
Mat = csvread('data.csv'); disp(Mat);
2) Read part of the data with specification of the start index. What is important is the data is accessed from index 0 in the direction of row and column.
Mat = csvread('data.csv', 1, 2); disp(Mat);
Result showing below.
7 8 1 2
3) Read only the specified range.
Mat = csvread('data.csv', 0, 1, [0,1,2,2]); disp(Mat);
Note the starting index you specified in the second and third parameters is the same with the first two arguments in the fourth parameter matrix.
2、 How to read a CSV file containing string data items.
Create a file, named as 'datastr.csv' if you like. Add the following data.
1, 2, 3, Mine 4, 5, 6, Yours 7, 8, 9, His
One who try to use csvread shall encounter frustration. :(
I propose another solution to solve this case.
fid = fopen('datastr.csv'); dcells = textscan(fid, '%f, %f, %f, %s'); fclose(fid); dcellneeds = dcells(1:3); Mat = cell2mat(dcellneeds); disp(Mat);
The result showing below.
1 2 3 4 5 6 7 8 9
Try it and good luck. :)
相关文章推荐
- Matlab:如何读取CSV文件以及如何读取带有字符串数据项的CSV文件
- 如何用MATLAB读取csv文件
- matlab读取csv文件存在字符串的问题(2014.5.8)
- matlab读取csv文件存在字符串的问题(2014.5.8)
- Python中如何读取CSV文件
- MATLAB如何读取excel文件中的数据?
- mysql导入导出csv文件语句,以及建表时候如何设置主键的三种方法
- 如何将视差数据保存为 txt 数据文件以便在 Matlab 中读取分析?
- 如何利用C/C++逐行读取txt文件中的字符串(可以顺便实现文本文件的复制)
- 如何读取csv文件的内容
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- Unity3D如何读取保存XML,以及用U3D内置方式保存文件
- Matlab中如何读出写入图像文件以及对图像的简单处理
- Matlab读取CSV文件
- 读取指定文件夹中所有文件名以及文件路径,并读到matlab
- matlab中如何读取TXT数据文件
- matlab中如何读取TXT数据文件
- matlab中如何读取TXT数据文件中指定行的数据?
- R语言-批量读取数据文件以及提取字符串中的数字
- 如何把字符串写入SD卡文件中,如何读取SD卡文件的数据