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

Thread Subject: reading file with variable number of columns

Subject: reading file with variable number of columns

From: Lee Sander

Date: 09 Aug, 2007 22:38:49

Message: 1 of 2

Hi,
I have a file with the following format

john 0.1 0.05 ...
jill 0.25 ...
...

essentially, first column of every row is a string and then after that
there are
variable numbers between 0 to 1 such that the number of columns are
different.
All I want to do is read line by line and then simply plot the
values.
how do I do this?

many thanks
lee

Subject: reading file with variable number of columns

From: us

Date: 09 Aug, 2007 23:23:05

Message: 2 of 2

Lee Sander:
<SNIP formatted text input evergreen...

one of the solutions is outlined below
note: <nan>s are used to indicate rows without any numbers

% the contents of file <foo.txt> according to your spec
% note: various number representations and an anomaly
a 1 2
bb 1.1 2.2 3.3
ccc 10.01
d4
e_5 100.11e-1
f .1e2 .2e3 0.3e4

% the engine
     fnam='foo.txt'; % <- use your file name
     rexname='^[^\s]+';
     rexdata='(?<=\s).+';
     s=textread(fnam,'%s','delimiter','\n');
     t=regexp(s,rexname,'match');
     t=[t{:}].';
     n=regexp(s,rexdata,'match');
     n(cellfun(@isempty,n))={'nan'};
     n=[n{:}].';
     nd=cellfun(@(x) sscanf(x,'%f').',n,'uni',false);
% the result
     type(fnam);
     [t,n]
     nd{:}

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
evergreen us 30 Jan, 2008 20:29:44
file io us 09 Aug, 2007 19:25:12
textread us 09 Aug, 2007 19:25:12
code us 09 Aug, 2007 19:25:12
regular expression us 09 Aug, 2007 19:25:12
regexp us 09 Aug, 2007 19:25:12
cellfun us 09 Aug, 2007 19:25:12
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