MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   在MATLAB中逐行读取文本文件 (https://www.labfans.com/bbs/showthread.php?t=23438)

poster 2019-12-10 20:41

在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的距离的行保存在新文件中。字符串字段也应该被复制。谢谢



[B]回答:[/B]

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

>> [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] 然后,您可以对数字数据执行所需的任何处理,然后使用[URL="https://www.mathworks.com/help/matlab/ref/xlswrite.html"]xlswrite[/URL]将数据行的子集重新保存到新文件中。这是一个例子:

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

[url=https://stackoverflow.com/questions/2856417]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 01:08

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