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

Thread Subject: reading data from a tex file

Subject: reading data from a tex file

From: Dimitar Dimitrov

Date: 31 Jul, 2007 15:49:21

Message: 1 of 3

Hello,

I have a text file with the following format:

% J1 J2
1 2
3 4
5 6
7 8
% J1 J2 J3
1 2 3
4 5 6
7 8 9
...

After each % mark, the data has different dimensions.
I want to read this file, and save it in "n" different matrices (for the example above n=2)

Mat(1).H = [1 2 ; 3 4 ; 5 6 ; 7 8]
Mat(2).H = [1 2 3 ; 4 5 6 ; 7 8 9]
...

Is there a simple way to do that.
Thank you!

Subject: reading data from a tex file

From: us

Date: 31 Jul, 2007 16:05:06

Message: 2 of 3

Dimitar Dimitrov:
<SNIP yet another data import evergreen...

one of the solutions is outlined below

% contents of file <foo.txt>
% J1 J2
1 2
3 4
5 6
7 8
% J1 J2 J3
1 2 3
4 5 6
7 8 9

% the engine
     fnam=foo.txt';
     a=textread(fnam,'%s','delimiter','','whitespace','');
     an=cell(numel(a),1);
for i=1:numel(a)
     [an{i,1:3}]=sscanf(a{i},'%f');
end
     ixs=[0,cellfun(@isempty,an(:,3)).',0];
     ib=strfind(ixs,[0,1]);
     ie=strfind(ixs,[1,0])-1;
if ~isempty(ib)
     nb=numel(ib);
     rb=cell(nb,1);
for i=1:nb
     nc=an{ib(i),2};
     rb{i,1}=reshape(cat(1,an{ib(i):ie(i),1}).',nc,[]).';
end
end
% the result
     type(fnam);
     rb{:}

us

Subject: reading data from a tex file

From: Dimitar Dimitrov

Date: 31 Jul, 2007 18:02:58

Message: 3 of 3

"us " <us@neurol.unizh.ch> wrote in message <f8nmji$rcb$1@fred.mathworks.com>...
> Dimitar Dimitrov:
> <SNIP yet another data import evergreen...
>
> one of the solutions is outlined below
>
> % contents of file <foo.txt>
> % J1 J2
> 1 2
> 3 4
> 5 6
> 7 8
> % J1 J2 J3
> 1 2 3
> 4 5 6
> 7 8 9
>
> % the engine
> fnam=foo.txt';
> a=textread(fnam,'%s','delimiter','','whitespace','');
> an=cell(numel(a),1);
> for i=1:numel(a)
> [an{i,1:3}]=sscanf(a{i},'%f');
> end
> ixs=[0,cellfun(@isempty,an(:,3)).',0];
> ib=strfind(ixs,[0,1]);
> ie=strfind(ixs,[1,0])-1;
> if ~isempty(ib)
> nb=numel(ib);
> rb=cell(nb,1);
> for i=1:nb
> nc=an{ib(i),2};
> rb{i,1}=reshape(cat(1,an{ib(i):ie(i),1}).',nc,[]).';
> end
> end
> % the result
> type(fnam);
> rb{:}
>
> us

"us",
Thank you for the prompt reply!
This solves the problem.

Dimitar




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:27:08
textread us 31 Jul, 2007 12:06:18
cellfun us 31 Jul, 2007 12:06:18
file io us 31 Jul, 2007 12:06:18
code us 31 Jul, 2007 12:06:18
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