Import from .txt or .csv

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 http://www.mathworks.com/matlabcentral/fileexchange/10946. 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?
{[],'12/31/1999','01/03/2000';'88553510',1,1;'88579Y10',1,1;'00282410',1,1;}
shows up as
{[],'12/31/1999','01/03/2000';88553510,1,1;'88579Y10',1,1;00282410,1,1;}
Thanks a lot, Brian

 Réponse acceptée

Ken Atwell
Ken Atwell le 19 Avr 2012

0 votes

Assuming the CSV file looks like this:
,12/31/1999,01/03/2000
88553510,1,1
88579Y10,1,1
00282410,1,1
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)
fclose(f);
This will create a 1x3 cell array, which each member of the cell array being one of the rows of your CSV data.

6 commentaires

Brian
Brian le 20 Avr 2012
Thanks for the answer Ken. I am fairly familiar with textscan and use it a lot for certain types of files. The file I'm importing at the moment is 3200 columns wide, which makes textscan a less than optimal option. Maybe there is a way to repeat a field without typing out %n 3200 times. If so, then textscan would be usable.
Thanks,
Brian
Titus Edelhofer
Titus Edelhofer le 20 Avr 2012
Hi Brian,
if you know the number (e.g. by reading the header line and counting the number of delimiters, you can use
numberOfColumns = 3200; % or counted
C = textscan(f, repmat('%s ', 1, numberOfColumns), ...)
Titus
Brian
Brian le 30 Avr 2012
Titus how would I go about counting the number of delimiters before I import the file? If this is fairly simple this method would work excellent!
Ken Atwell
Ken Atwell le 2 Mai 2012
You would be able to get a column count by using fopen, fgetl, and then counting the command with a command line:
numColumns = sum(lineIjustReadIn==',');
Then reset to the top of the file (fseek). Finally, now knowing the number of columns, use textscan.
Geoff
Geoff le 2 Mai 2012
The number of columns is the number of commas PLUS ONE! =)
Brian
Brian le 3 Mai 2012
Titus if you make your comment into an answer I will accept it.
Thanks a bunch,
Brian

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Live Scripts and Functions dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by