Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: .csv - file with numbers & text

Subject: .csv - file with numbers & text

From: Daniel Miller

Date: 24 Jul, 2008 08:27:02

Message: 1 of 4

Hy,

My problem:
Is there a way to upload a .csv file into matlab with
contains numbers and text? (e.g. row 1, 2, 4 contains only
numbers, row 3,5,6 only text)


Thanks

Subject: .csv - file with numbers & text

From: NZTideMan

Date: 24 Jul, 2008 08:54:32

Message: 2 of 4

On Jul 24, 8:27=A0pm, "Daniel Miller" <bestell...@espenmoos-buch.ch>
wrote:
> Hy,
>
> My problem:
> Is there a way to upload a .csv file into matlab with
> contains numbers and text? (e.g. row 1, 2, 4 contains only
> numbers, row 3,5,6 only text)
>
> Thanks

One way is to save it as an Excel file and load using xlsread.
Then the numbers will be in an array and the text in a cell array.

Otherwise, you need to read it line by line using fgetl, for example.

Subject: .csv - file with numbers & text

From: us

Date: 24 Jul, 2008 09:03:22

Message: 3 of 4

"Daniel Miller":
<SNIP mixed blessing...

> Is there a way to upload a .csv file into matlab with
> contains numbers and text...

one of the (other) solutions

% assume this is the contents of your .csv
%{
1,2,3
abc,def,ghi
4,5,6
x,yy,zzz
%}

% the engine
     c=textread('foo.csf','%s');
     n=cellfun(@(x) sscanf(x,'%g,').',a,'uni',false);
     ie=cellfun(@isempty,n);
     n=cat(1,n{~ie});
     s=regexp(c(ie),',','split');
     s=cat(1,s{:});
% the result
     disp(n);
     disp(s);
%{
     1 2 3
     4 5 6
     'abc' 'def' 'ghi'
     'x' 'yy' 'zzz'
%}

us

Subject: .csv - file with numbers & text

From: us

Date: 24 Jul, 2008 09:08:03

Message: 4 of 4

"us ":
<SNIP made a stupid typo in his/her last reply...

> "Daniel Miller":
> <SNIP mixed blessing...

the correct snippet...

% assume this is the contents of your .csv
%{
1,2,3
abc,def,ghi
4,5,6
x,yy,zzz
%}

% the engine
     c=textread('foo.csv','%s');
     n=cellfun(@(x) sscanf(x,'%g,').',c,'uni',false);
     ie=cellfun(@isempty,n);
     n=cat(1,n{~ie});
     s=regexp(c(ie),',','split');
     s=cat(1,s{:});
% the result
     disp(n);
     disp(s);
%{
     1 2 3
     4 5 6
     'abc' 'def' 'ghi'
     'x' 'yy' 'zzz'
%}

sorry...
us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
cat us 24 Jul, 2008 05:05:22
cellfun us 24 Jul, 2008 05:05:21
textread us 24 Jul, 2008 05:05:21
regexp us 24 Jul, 2008 05:05:21
code us 24 Jul, 2008 05:05:21
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics