在matlab中从CSV文件中读取特定列 [英] Reading specific column from CSV file in matlab

查看:8254
本文介绍了在matlab中从CSV文件中读取特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在matlab中读取CSV文件。我只想读第二列,但下面的代码打印出CSV文件中的一切。我有哪些参数或功能引入,使其只读取第二列

  FILENAME ='C:\Users\ Desktop\Results.csv'; 

fid = fopen(FILENAME,'rt');
a = textscan(fid,'%s','HeaderLines',1,'Delimiter',',');
fclose(fid);
celldisp(一)


解决方案

有几个方法:


  1. 使用 cvsread

    假设您在文件 1

    中有 N 行:

      a = csvread(FILENAME,0,1,[0 1 N-1 1]); 


  2. 您也可以考虑 xlsread

      a = xlsread(FILENAME,'B:B'); 

    查看具体例如中的 xlsread 文档。


  3. 另一个选择是 dlmread

      A = dlmread( FILENAME,',',[0 1 N-1 1]); 







1 - 一个很好的(和快速)的方法来计算在Matlab文件中的行可以的这个回答由Rody Oldenhuis


I am trying to read a CSV file in matlab. I just want to read the second column but the code below prints out everything on CSV file. What parameters or functions I have to introduce to make it read just the second column

FILENAME = 'C:\Users\Desktop\Results.csv';

fid = fopen(FILENAME, 'rt');
a = textscan(fid, '%s', 'HeaderLines',1,'Delimiter',',');
fclose(fid);
celldisp(a)

解决方案

There are several ways:

  1. Using cvsread:
    Assuming you have N rows in the file1:

    a = csvread( FILENAME, 0, 1, [0 1 N-1 1 ] );
    

  2. You might also consider xlsread

    a = xlsread( FILENAME, 'B:B' );  
    

    See specific example on the xlsread doc.

  3. Another option is dlmread

    a = dlmread( FILENAME, ',', [0 1 N-1 1] );
    


1 - A nice (and fast) way to count the number of lines in the file in Matlab can be found in this answer by Rody Oldenhuis.

这篇关于在matlab中从CSV文件中读取特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆