Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
回复
 
主题工具 显示模式
旧 2019-12-10, 20:41   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 在MATLAB中逐行读取文本文件

我有一个CSV文件,我想读取此文件并在每行上进行一些预先计算,以查看该行是否对我有用,如果是,我将其保存到新的CSV文件中。有人可以给我一个例子吗?更详细地讲,这就是我的数据的样子:(string,float,float)数字是坐标。

ABC,51.9358183333333,4.183255 ABC,51.9353866666667,4.1841 ABC,51.9351716666667,4.184565 ABC,51.9343083333333,4.186425 ABC,51.9343083333333,4.186425 ABC,51.9340916666667,4.18688333333333 基本上我想将具有大于50或50的距离的行保存在新文件中。字符串字段也应该被复制。谢谢



回答:

您实际上可以使用xlsread来完成此操作。在将上面的样本数据首先放置在文件'input_file.csv' ,下面是一个示例,说明如何从xlsread的三个输出中获取文件中的数值,文本值和原始数据:

>> [numData,textData,rawData] = xlsread('input_file.csv') numData = % An array of the numeric values from the file 51.9358 4.1833 51.9354 4.1841 51.9352 4.1846 51.9343 4.1864 51.9343 4.1864 51.9341 4.1869 textData = % A cell array of strings for the text values from the file 'ABC' 'ABC' 'ABC' 'ABC' 'ABC' 'ABC' rawData = % All the data from the file (numeric and text) in a cell array 'ABC' [51.9358] [4.1833] 'ABC' [51.9354] [4.1841] 'ABC' [51.9352] [4.1846] 'ABC' [51.9343] [4.1864] 'ABC' [51.9343] [4.1864] 'ABC' [51.9341] [4.1869] 然后,您可以对数字数据执行所需的任何处理,然后使用xlswrite将数据行的子集重新保存到新文件中。这是一个例子:

index = sqrt(sum(numData.^2,2)) >= 50; % Find the rows where the point is % at a distance of 50 or greater % from the origin xlswrite('output_file.csv',rawData(index,:)); % Write those rows to a new file

更多&回答...
poster 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 05:13


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.