From: roya olyazadeh on
I want to read this text in matlab

C 1 1000.000000 1000.000000 !
C 4 878.926000 1021.071000 ! !
D 1 2 122.286 0.002
D 1 5 96.954 0.002
D 1 3 190.522 0.002
D 1 6 116.255 0.002
D 1 4 122.846 0.002
A 1 2 5 35 17 32.00 3.0
A 1 2 3 46 52 06.00 3.0
A 1 2 6 36 56 38.00 3.0

I could read it with textread and textscan. But I should seprate format to 3 different texts. In fact I need to read them in one file like above. and also line by line.
For example if matlab read line 1 and the first row is C then do this(ut 1000 and 1000 in matrix X ) . If D then do this ( put 1 2 122.286 in these matrix A B Distance) . and...
Can any one know how I can do it.
I really need to do it soon for my project
tnx
From: dpb on
roya olyazadeh wrote:
> I want to read this text in matlab
> C 1 1000.000000 1000.000000 !
> C 4 878.926000 1021.071000 ! !
> D 1 2 122.286 0.002
> D 1 5 96.954 0.002
> D 1 3 190.522 0.002
> D 1 6 116.255 0.002
> D 1 4 122.846 0.002
> A 1 2 5 35 17 32.00 3.0
> A 1 2 3 46 52 06.00 3.0
> A 1 2 6 36 56 38.00 3.0
>
> I could read it with textread and textscan. But I should seprate format
> to 3 different texts. In fact I need to read them in one file like
> above. and also line by line. For example if matlab read line 1 and the
> first row is C then do this(ut 1000 and 1000 in matrix X ) . If D then
> do this ( put 1 2 122.286 in these matrix A B Distance) . and...
> Can any one know how I can do it. I really need to do it soon for my
> project
> tnx

I'd probably just parse it line by line and create the datasets. If
need to do this multiple times and the file is sizable that time to
process is noticeable, create new files that have the separate data in
them in easier-to-process format (like .mat file, say)

--
From: roya olyazadeh on
dpb <none(a)non.net> wrote in message <hrekt8$iiq$1(a)news.eternal-september.org>...
> roya olyazadeh wrote:
> > I want to read this text in matlab
> > C 1 1000.000000 1000.000000 !
> > C 4 878.926000 1021.071000 ! !
> > D 1 2 122.286 0.002
> > D 1 5 96.954 0.002
> > D 1 3 190.522 0.002
> > D 1 6 116.255 0.002
> > D 1 4 122.846 0.002
> > A 1 2 5 35 17 32.00 3.0
> > A 1 2 3 46 52 06.00 3.0
> > A 1 2 6 36 56 38.00 3.0
> >
> > I could read it with textread and textscan. But I should seprate format
> > to 3 different texts. In fact I need to read them in one file like
> > above. and also line by line. For example if matlab read line 1 and the
> > first row is C then do this(ut 1000 and 1000 in matrix X ) . If D then
> > do this ( put 1 2 122.286 in these matrix A B Distance) . and...
> > Can any one know how I can do it. I really need to do it soon for my
> > project
> > tnx
>
> I'd probably just parse it line by line and create the datasets. If
> need to do this multiple times and the file is sizable that time to
> process is noticeable, create new files that have the separate data in
> them in easier-to-process format (like .mat file, say)
>
> I could not to do it in separate file . It must be in one file and can read line by line
do you know how to do it ?tnx
From: dpb on
roya olyazadeh wrote:
> dpb <none(a)non.net> wrote in message
> <hrekt8$iiq$1(a)news.eternal-september.org>...
>> roya olyazadeh wrote:
>> > I want to read this text in matlab
>> > C 1 1000.000000 1000.000000 !
>> > C 4 878.926000 1021.071000 ! !
>> > D 1 2 122.286 0.002
>> > D 1 5 96.954 0.002
>> > D 1 3 190.522 0.002
>> > D 1 6 116.255 0.002
>> > D 1 4 122.846 0.002
>> > A 1 2 5 35 17 32.00 3.0
>> > A 1 2 3 46 52 06.00 3.0
>> > A 1 2 6 36 56 38.00 3.0
>> > > I could read it with textread and textscan. But I should seprate
>> format > to 3 different texts. In fact I need to read them in one file
>> like > above. and also line by line. For example if matlab read line 1
>> and the > first row is C then do this(ut 1000 and 1000 in matrix X ) .
>> If D then > do this ( put 1 2 122.286 in these matrix A B Distance)
>> . and...
....

>> I'd probably just parse it line by line and create the datasets. If
>> need to do this multiple times and the file is sizable that time to
>> process is noticeable, create new files that have the separate data in
>> them in easier-to-process format (like .mat file, say)
>>
>> I could not to do it in separate file . It must be in one file and can
>> read line by line do you know how to do it?

doc fgetl

fid = fopen('yourfile');
while ~feof(fid)
s = fgetl(fid);
switch s(1)
case {'C'}
x = sscanf(s(2:end), '%%d%f%f)';
case ('D')
[a, b, d] = strread(s(2:end),'%d %d %f*[^\n]');
case ('A')
...
end
end
fid=fclose(fid);

Most simplistic manner -- this would need to handle first case outside
the loop and concatenate new rows to each array or preallocate the
arrays and index into them keeping track of a separate row count for
each case but those are just bookkeeping details.

Alternatively, if the file isn't huge, read the whole file into one big
string array and locate each line via find() or similar and use that
logical indexing to process all rows of the same type in a chunk...

Many possible choices...

--
From: dpb on
dpb wrote:
> roya olyazadeh wrote:
....

>>> I could not to do it in separate file . It must be in one file and
>>> can read line by line do you know how to do it?
>
> doc fgetl
>
> fid = fopen('yourfile');
> while ~feof(fid)
> s = fgetl(fid);
> switch s(1)
> case {'C'}
> x = sscanf(s(2:end), '%%d%f%f)';

woops, typo--the second "%" should been "*" to skip the integer field
there...
x = sscanf(s(2:end), '%*d%f%f)';

> case ('D')
> [a, b, d] = strread(s(2:end),'%d %d %f*[^\n]');

and that extra one belonged in here in the skip to endline part...

[a, b, d] = strread(s(2:end),'%d %d %f%*[^\n]');

> case ('A')
> ...
> end
> end
> fid=fclose(fid);
>
> Most simplistic manner -- this would need to handle first case outside
> the loop and concatenate new rows to each array

That would be something like x = [x; sscanf(....)]; for the single
array; not so simple in the multiple array case--there you'll really
want to preallocate if you simply must have the separate variables and
go at it this way.

Oh, and I see absolutely no reason you couldn't do it in multiple files
either on the git-go or for followup once you've initially parsed the
file. It would be quite simple to create a file w/ the same name but a
letter indicator included to keep track of them, etc.

In fact, one alternative would be to use the idea of the above snippet
and instead of parsing the line, write each line to a separate file and
then process each file as a block.

Or, do it in memory and simply concatenate the lines as suggested and do
a slurp on those string arrays to build the numeric ones...probably much
more efficient overall.

--