From: roya olyazadeh on
dpb <none(a)non.net> wrote in message <hreof1$qjp$1(a)news.eternal-september.org>...
> 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...
>
> --

Tnx for your help . I tried it. but results are not matrix

Result :
ns =

1.0e+003 *

0.0040
0.8789
1.0211
0.0330
0.0330

a =

3


b =

4


d =

2


Program:

fid=fopen('Exm0.txt')
head_lines = 0;
while ~feof(fid)
head_lines = head_lines+1;
tline = fgetl(fid);
answer = findstr(tline,'END OF HEADER');
if ~ischar(tline), break, end

switch tline(1)
case {'C' | 'c'}
ns = sscanf(tline(2:end), '%d %f %f %s %s %*[^\n]')

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

end
fclose(fid);
From: dpb on
roya olyazadeh wrote:
> dpb <none(a)non.net> wrote in message
> <hreof1$qjp$1(a)news.eternal-september.org>...
>> 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...
>>
>> --
>
> Tnx for your help . I tried it. but results are not matrix
>
> Result : ns =
>
> 1.0e+003 *
>
> 0.0040
> 0.8789
> 1.0211
> 0.0330
> 0.0330
>
> a =
>
> 3
>
>
> b =
>
> 4
>
>
> d =
>
> 2
>
>
> Program:
>
> fid=fopen('Exm0.txt')
> head_lines = 0;
> while ~feof(fid)
> head_lines = head_lines+1;
> tline = fgetl(fid);
> answer = findstr(tline,'END OF HEADER');
> if ~ischar(tline), break, end
> switch tline(1)
> case {'C' | 'c'}
> ns = sscanf(tline(2:end), '%d %f %f %s %s %*[^\n]')
> case ('D')
> [a, b, d] = strread(tline(2:end),'%d %d %f %*[^\n]');
> case ('A')
> [a, b, d] = strread(tline(2:end),'%d %d %f %*[^\n]');
> end
> end
> fclose(fid);

Well, you didn't read the comments -- this is _NOT_ a complete code
simply an outline of how to deal w/ the various cases.

As noted, you need to preallocate an array for each array you want and
add to it each pass through the loop. What this does is to return the
values for each line overwriting the previous values every time.

Or, alternatively, and probably better would be to create the string
arrays for each subset that is different in parsing and then use strread
on them where each is of the same form.

Looking at what you wrote above, it doesn't appear there's any
difference between a 'D' and an 'A' -- is that correct? If so, start w/
something like

a = ''; c = a; d = a; % create some empty string arrays
> fid=fopen('Exm0.txt')
> head_lines = 0;
> while ~feof(fid)
> head_lines = head_lines+1;
> tline = fgetl(fid);
> answer = findstr(tline,'END OF HEADER');

What is the above supposed to do? 'answer' is never used so...

> if ~ischar(tline), break, end
switch upper(tline(1)); % Simpler to make it upper than enumerate
case {'C'}
c = strvcat(c,tline(2:end));
case ('A' | 'D')
a = strvcat(a,tline(2:end));
case otherwise
disp(['Unknown case: tline(1)])
> end
> end
> fclose(fid);

If there is something different about A and D then of course keep them
separate and add the other case back.

Once you have these arrays you can use textscan() on them and build the
arrays in one swell foop...

--
From: us on
"roya olyazadeh" <roya2543(a)gmail.com> wrote in message <hreirk$69e$1(a)fred.mathworks.com>...
> 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

one of the many solutions

% the data
fnam=foo.txt'; % <- your file name
% - parser template
tmpl={
'C' 'Cval' '%c %d %f %f' 2:3
'D' 'Dval' '%c %d %d %f %f' 2:4
};
% the engine
s=textread(fnam,'%s','delimiter','','whitespace',' ');
s=strtrim(strrep(s,'!',''));
for i=1:size(tmpl,1)
ix=strncmp(s(:,1),tmpl{i,1},1);
v=cellfun(@(x) sscanf(x,tmpl{i,3}),s(ix),'uni',false);
v=cat(2,v{:}).';
r.(tmpl{i,2})=v(:,tmpl{i,4});
end
% the result
r.Cval
r.Dval
%{
% r.Cval =

1 1000 1000
4 878.93 1021.1
% r.Dval =
1 2 122.29
1 5 96.954
1 3 190.52
1 6 116.26
1 4 122.85
%}

us
From: roya olyazadeh on

> a = ''; c = a; d = a; % create some empty string arrays
> > fid=fopen('Exm0.txt')
> > head_lines = 0;
> > while ~feof(fid)
> > head_lines = head_lines+1;
> > tline = fgetl(fid);
> > answer = findstr(tline,'END OF HEADER');
>
> What is the above supposed to do? 'answer' is never used so...
>
> > if ~ischar(tline), break, end
> switch upper(tline(1)); % Simpler to make it upper than enumerate
> case {'C'}
> c = strvcat(c,tline(2:end));
> case ('A' | 'D')
> a = strvcat(a,tline(2:end));
> case otherwise
> disp(['Unknown case: tline(1)])
> > end
> > end
> > fclose(fid);
>
> If there is something different about A and D then of course keep them
> separate and add the other case back.
>
> Once you have these arrays you can use textscan() on them and build the
> arrays in one swell foop...
>
> --



I read your comments tnx. But I am not professional in MATLAB.
I use this : tline = fgetl(fid);
answer = findstr(tline,'END OF HEADER');
if ~ischar(tline), break, end

to display my text after reading.
case A and D must be in separate array because I need to do computation separately.

and after running I have d and a and c
Are a and c and d matrix? Why couldn't I use them like this?? degree=a(1,4)
'degree' is empty.

I tried to use textscan but where?
D = textscan(d,'%d %d %f %f %*[^\n]'); d is array from strvcat

again tnx for your help.

clc
a = ''; c = a; d = a;
fid=fopen('Exm0.txt')
head_lines = 0;


while ~feof(fid)
head_lines = head_lines+1;
tline = fgetl(fid);
answer = findstr(tline,'END OF HEADER');
if ~ischar(tline), break, end
disp(tline)

switch tline(1)
case {'C' | 'c'}
c = strvcat(c,tline(2:end));
case ('D')

d = strvcat(d,tline(2:end));

case ('A')

a=strvcat(a,tline(2:end));
end

end
a
d
c
degree=a(1,4)

%end of program
From: roya olyazadeh on
"us " <us(a)neurol.unizh.ch> wrote in message <hrfjnf$pr7$1(a)fred.mathworks.com>...
> "roya olyazadeh" <roya2543(a)gmail.com> wrote in message <hreirk$69e$1(a)fred.mathworks.com>...
> > 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
>
> one of the many solutions
>
> % the data
> fnam=foo.txt'; % <- your file name
> % - parser template
> tmpl={
> 'C' 'Cval' '%c %d %f %f' 2:3
> 'D' 'Dval' '%c %d %d %f %f' 2:4
> };
> % the engine
> s=textread(fnam,'%s','delimiter','','whitespace',' ');
> s=strtrim(strrep(s,'!',''));
> for i=1:size(tmpl,1)
> ix=strncmp(s(:,1),tmpl{i,1},1);
> v=cellfun(@(x) sscanf(x,tmpl{i,3}),s(ix),'uni',false);
> v=cat(2,v{:}).';
> r.(tmpl{i,2})=v(:,tmpl{i,4});
> end
> % the result
> r.Cval
> r.Dval
> %{
> % r.Cval =
>
> 1 1000 1000
> 4 878.93 1021.1
> % r.Dval =
> 1 2 122.29
> 1 5 96.954
> 1 3 190.52
> 1 6 116.26
> 1 4 122.85
> %}
>
> This error occurred after running

Too many inputs.

Error in ==> secndcor at 13
v=cellfun(@(x) sscanf(x,tmpl{i,3}),s(ix),'uni',false);

and one thing , file size is changeable. sometimes maybe 100 lines . sometimes they
are not in order like this :
D 1 2 122.286 0.002
A 1 2 5 35 17 32.00 3.0
C 1 1000.000000 1000.000000 !
D 1 3 190.522 0.002
C 4 878.926000 1021.071000 ! !

What is the solution ?