Import from .txt or .csv

9 views (last 30 days)
Brian on 19 Apr 2012
I am importing some large .csv files where I have textual column headers and row headers. I am using the readtext() function from the FEX for mixed-type files I am doing this to import the matrix into a large cell in Matlab from which I can separate my text and numeric data. My column headers are dates, which are fine and show up as text for my import. My row headers are financial CUSIPS, which sometimes contain a letter and show up as text, and other times contain all numbers (and begin with 0's) and then show up as numbers.
My questions - how can I import this typed of mixed data file preserving the format of all of my original data?
shows up as
Thanks a lot, Brian

Accepted Answer

Ken Atwell
Ken Atwell on 19 Apr 2012
Assuming the CSV file looks like this:
textscan (ship with MATLAB) should be sufficient. To skip the first (header) line, and then import three columns of comma-separated data, with the first row a text string and the other two rows containing numeric data:
f = fopen ('data.csv');
C = textscan(f, '%s %n %n', 'Delimiter', ',', 'HeaderLines', 1)
This will create a 1x3 cell array, which each member of the cell array being one of the rows of your CSV data.
Brian on 3 May 2012
Titus if you make your comment into an answer I will accept it.
Thanks a bunch,

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by