Reading first column of a .csv file

30 ビュー (過去 30 日間)
Zack Bayhan
Zack Bayhan 2015 年 8 月 29 日
回答済み: Sahil Negi 2021 年 10 月 18 日
Hi everyone, I'm having some difficulty getting the information I'm looking for extracted. I would like to pull everything from the first column from rows 2 to the end. When I use txtread I extract the entire document, I've tried a few other read functions however they produced errors. Any help would be greatly appreciated, I've attached the .csv file and the script producing the .csv file. Thanks in advanced and hope you all have a great weekend. Zack
fu=['https://raw.githubusercontent.com/datasets/s-and-p-500-companies/master/data/constituents-financials.csv'];
urlwrite(fu,'Symbols.csv')
symbols=textread('symbols.csv','%s');

採用された回答

Star Strider
Star Strider 2015 年 8 月 30 日
I would use xlsread:
[~,symbols] = xlsread('Symbols.csv', 'A:A');
This imports a (497x1) cell of the symbols in Column A, plus the column header ‘Symbol’, so you may only want to use the last 496.

その他の回答 (3 件)

Walter Roberson
Walter Roberson 2015 年 8 月 30 日
fid = fopen('symbols.csv', 'rt');
datacell = textscan(fid, '%[^,]%*[^\n]', 'HeaderLines', 1);
fclose(fid);
symbols = datacell{1};

Zack Bayhan
Zack Bayhan 2015 年 8 月 30 日
Awesome thanks for the help once again, Star Strider! Worked like a charm, and was able to drop the header out so hopefully now I'll be able to use this data.
  1 件のコメント
Star Strider
Star Strider 2015 年 8 月 30 日
As always, my pleasure!

サインインしてコメントする。


Sahil Negi
Sahil Negi 2021 年 10 月 18 日
You can use importdata as shown below:
symbols = importdata('Symbols.csv');
column_1 = symbols.data(:, 1);
This should get your first column without the header.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by