Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how can I read text file in matlab ?

Subject: how can I read text file in matlab ?

From: roya olyazadeh

Date: 30 Apr, 2010 12:36:04

Message: 1 of 27

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

Subject: how can I read text file in matlab ?

From: dpb

Date: 30 Apr, 2010 13:09:09

Message: 2 of 27

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)

--

Subject: how can I read text file in matlab ?

From: roya olyazadeh

Date: 30 Apr, 2010 13:31:04

Message: 3 of 27

dpb <none@non.net> wrote in message <hrekt8$iiq$1@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

Subject: how can I read text file in matlab ?

From: dpb

Date: 30 Apr, 2010 14:09:50

Message: 4 of 27

roya olyazadeh wrote:
> dpb <none@non.net> wrote in message
> <hrekt8$iiq$1@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...

--

Subject: how can I read text file in matlab ?

From: dpb

Date: 30 Apr, 2010 15:43:27

Message: 5 of 27

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.

--

Subject: how can I read text file in matlab ?

From: roya olyazadeh

Date: 30 Apr, 2010 16:22:04

Message: 6 of 27

dpb <none@non.net> wrote in message <hreof1$qjp$1@news.eternal-september.org>...
> roya olyazadeh wrote:
> > dpb <none@non.net> wrote in message
> > <hrekt8$iiq$1@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);

Subject: how can I read text file in matlab ?

From: dpb

Date: 30 Apr, 2010 18:03:58

Message: 7 of 27

roya olyazadeh wrote:
> dpb <none@non.net> wrote in message
> <hreof1$qjp$1@news.eternal-september.org>...
>> roya olyazadeh wrote:
>> > dpb <none@non.net> wrote in message >
>> <hrekt8$iiq$1@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...

--

Subject: how can I read text file in matlab ?

From: us

Date: 30 Apr, 2010 21:57:03

Message: 8 of 27

"roya olyazadeh" <roya2543@gmail.com> wrote in message <hreirk$69e$1@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

Subject: how can I read text file in matlab ?

From: roya olyazadeh

Date: 1 May, 2010 06:05:22

Message: 9 of 27


> 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

Subject: how can I read text file in matlab ?

From: roya olyazadeh

Date: 1 May, 2010 06:24:05

Message: 10 of 27

"us " <us@neurol.unizh.ch> wrote in message <hrfjnf$pr7$1@fred.mathworks.com>...
> "roya olyazadeh" <roya2543@gmail.com> wrote in message <hreirk$69e$1@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 ?

Subject: how can I read text file in matlab ?

From: dpb

Date: 1 May, 2010 19:05:59

Message: 11 of 27

roya olyazadeh wrote:
>
...
> 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.

I don't follow, but ok...

> case A and D must be in separate array because I need to do computation
> separately.

But then you'll need some other place to store them besides the same
array names used previously.

> 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
...

OK, this parses the two cases you gave answers for -- it's kinda' klunky
because there isn't a convenient way to concatenate multiple arrays in a
single statement and assuming the arrays aren't terribly huge and you
don't have to do this a whole bunch of times I didn't preallocate arrays
and index not having an a priori count of maximum size.

x = []; a = x; b = x; d = x; % create some empty arrays
while ~feof(fid)
   s=fgetl(fid); % get a line
   if isempty(s), continue, end % skip any empty lines
   switch upper(s(1)) % make it upper for single test
     case {'C'}
       x = [x; sscanf(s(2:end), '%*d%f%f)')'];
     case ('A')
       % don't have a rule for this case...
     case ('D')
       % read into temporary variable then concat into arrays...
       [r,s,t] = strread(s(2:end),'%d%d%f%*[^\n]')
       a = [a;r]; b = [b;s]; d = [d;t]
     otherwise
       disp(['Unknown case: ' s(1)])
   end
end

I don't have a late enough version to debug us' anonymous functions
version -- it'll undoubtedly be somewhat cleaner if you do.

As for "being professional", it's "time in grade" -- if you're going to
use Matlab, spend some time reading the "Getting Started" section. It
will pay off in the longer run if your project is anything more than a
day long effort...

--

Subject: how can I read text file in matlab ?

From: roya olyazadeh

Date: 2 May, 2010 13:47:03

Message: 12 of 27


> OK, this parses the two cases you gave answers for -- it's kinda' klunky
> because there isn't a convenient way to concatenate multiple arrays in a
> single statement and assuming the arrays aren't terribly huge and you
> don't have to do this a whole bunch of times I didn't preallocate arrays
> and index not having an a priori count of maximum size.
>
> x = []; a = x; b = x; d = x; % create some empty arrays
> while ~feof(fid)
> s=fgetl(fid); % get a line
> if isempty(s), continue, end % skip any empty lines
> switch upper(s(1)) % make it upper for single test
> case {'C'}
> x = [x; sscanf(s(2:end), '%*d%f%f)')'];
> case ('A')
> % don't have a rule for this case...
> case ('D')
> % read into temporary variable then concat into arrays...
> [r,s,t] = strread(s(2:end),'%d%d%f%*[^\n]')
> a = [a;r]; b = [b;s]; d = [d;t]
> otherwise
> disp(['Unknown case: ' s(1)])
> end
> end
>
> I don't have a late enough version to debug us' anonymous functions
> version -- it'll undoubtedly be somewhat cleaner if you do.
>
> As for "being professional", it's "time in grade" -- if you're going to
> use Matlab, spend some time reading the "Getting Started" section. It
> will pay off in the longer run if your project is anything more than a
> day long effort...
>
> Now it works. Thank you very much .
Now I am moving to next part of my project. tnx again

clc
filename=input('Input the filename:','s');

fid=fopen(filename);
head_lines = 0;

x = []; at = x; fr = x; tt = x; deg=x;
min=x; sec=x; from=x; to=x; dist=x; var=x; var2=x;
stn=x; y=x; fix1=x; fix2=x;
i=0;j=0;h=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' }
       [q,r,s,t,u] = strread(tline(2:end), '%d %f %f %s %s %*[^\n]');
       stn=[stn;q]; x=[x;r]; y=[y,s]; fix1=[fix1;t]; fix2=[fix2;u];
       i=i+1;
      
     case ('D')
       [m, n, o, p] = strread(tline(2:end),'%d %d %f %f %*[^\n]');
       from = [from;m]; to = [to;n]; dist = [dist;o]; var = [var;p];
       j=j+1;
      
     case ('A')
       [a, b, c,d,e,f,g] = strread(tline(2:end),'%d %d %d %f %f %f %f %*[^\n]');
       at = [at;a]; fr = [fr;b]; tt = [tt;c];
       deg = [deg;d]; min = [min;e];sec = [sec;f];var2 = [var2;g];
       h=h+1;
     
    end
        
end

fclose(fid);

Subject: how can I read text file in matlab ?

From: dpb

Date: 2 May, 2010 13:55:22

Message: 13 of 27

roya olyazadeh wrote:
>
>> OK, this parses the two cases you gave answers for --
...
>> Now it works. Thank you very much .
> Now I am moving to next part of my project. tnx again
...
> filename=input('Input the filename:','s');
...

doc uigetfile % and friends may be convenient here...

--

Subject: how can I read text file in matlab ?

From: roya olyazadeh

Date: 3 May, 2010 12:09:22

Message: 14 of 27

dpb <none@non.net> wrote in message <hrk0bn$qhp$1@news.eternal-september.org>...
> roya olyazadeh wrote:
> >
> >> OK, this parses the two cases you gave answers for --
> ...
> >> Now it works. Thank you very much .
> > Now I am moving to next part of my project. tnx again
> ...
> > filename=input('Input the filename:','s');
> ...
>
> doc uigetfile % and friends may be convenient here...
>
> --


Hi
I have another question.. Can you tell me how can I add string to a matrix.
For this example I have this matrix :

from=[1 ;2 ;4; 6; 7; 8 ]
Now I want to add x to them so I have
from=[x1;x2;x4;x6;x7;x8]

tnx againt for your help.

Subject: how can I read text file in matlab ?

From: us

Date: 3 May, 2010 12:26:05

Message: 15 of 27

"roya olyazadeh"
> > 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 ?

which ML version do you have(?)...
can you upgrade(?)...
note: the order of your character tags does not matter...

us

Subject: how can I read text file in matlab ?

From: roya olyazadeh

Date: 3 May, 2010 12:35:19

Message: 16 of 27

"us " <us@neurol.unizh.ch> wrote in message <hrmfct$rhf$1@fred.mathworks.com>...
> "roya olyazadeh"
> > > 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 ?
>
> which ML version do you have(?)...
> can you upgrade(?)...
> note: the order of your character tags does not matter...
>
> us


I use matlab 7
I have another question.. Can you tell me how can I add string to a matrix.
For this example I have this matrix :

from=[1 ;2 ;4; 6; 7; 8 ]
Now I want to add x to them so I have
from=[x1;x2;x4;x6;x7;x8]
when I add them, Matlab gave me answers in numbers not in string like this
from=[x1;x2;x4;x6;x7;x8]

tnx againt for your help.

Subject: how can I read text file in matlab ?

From: dpb

Date: 3 May, 2010 13:08:38

Message: 17 of 27

roya olyazadeh wrote:
...

> from=[1 ;2 ;4; 6; 7; 8 ]
> Now I want to add x to them so I have from=[x1;x2;x4;x6;x7;x8]

In general, one may not want to do this...see

<http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F>

--

Subject: how can I read text file in matlab ?

From: us

Date: 3 May, 2010 19:02:21

Message: 18 of 27

"roya olyazadeh"
> I use matlab 7
> I have another question.. Can you tell me how can I add string to a matrix.
> For this example I have this matrix :

1) which version EXACTLY(!?)...

     help version;

2) do NOT start another thread within a thread...

us

Subject: how can I read text file in matlab ?

From: roya olyazadeh

Date: 4 May, 2010 01:08:04

Message: 19 of 27

"us " <us@neurol.unizh.ch> wrote in message <hrn6jt$fc1$1@fred.mathworks.com>...
> "roya olyazadeh"
> > I use matlab 7
> > I have another question.. Can you tell me how can I add string to a matrix.
> > For this example I have this matrix :
>
> 1) which version EXACTLY(!?)...
>
> help version;
>
> 2) do NOT start another thread within a thread...
>
> us


OK I fixed my first problem . Any way I think you dont want to help any more. I know how can I help matlab. there is no such thing for adding string to an integer matrix,
tnx

Subject: how can I read text file in matlab ?

From: roya olyazadeh

Date: 4 May, 2010 01:13:05

Message: 20 of 27

dpb <none@non.net> wrote in message <hrmi02$o4n$1@news.eternal-september.org>...
> roya olyazadeh wrote:
> ...
>
> > from=[1 ;2 ;4; 6; 7; 8 ]
> > Now I want to add x to them so I have from=[x1;x2;x4;x6;x7;x8]
>
> In general, one may not want to do this...see
>
> <http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F>
>
> --


I read this . I dont want to creat index. please just tell me how can I add string to an integer matrix ?????????????????
My project is comlicated I can not described it here. I need this matrix
from=[x1;x2;x4;x6;x7;x8] after reading data....
tell me what is teh fuction for this? I searched in matlab help there is no suchthing. I mean I could not find it.

Subject: how can I read text file in matlab ?

From: Walter Roberson

Date: 4 May, 2010 01:21:09

Message: 21 of 27

roya olyazadeh wrote:

> I read this . I dont want to creat index. please just tell me how can I
> add string to an integer matrix ?????????????????

That is impossible in Matlab. Numeric matrices in Matlab can never contain
character strings.

 >> from=[1 ;2 ;4; 6; 7; 8 ]

Possibly what you are looking for is:

 >> num2str(from,'x%d')
ans =
x1
x2
x4
x6
x7
x8

Subject: how can I read text file in matlab ?

From: roya olyazadeh

Date: 4 May, 2010 02:54:06

Message: 22 of 27

Walter Roberson <roberson@hushmail.com> wrote in message <hrnsq9$1va$2@canopus.cc.umanitoba.ca>...
> roya olyazadeh wrote:
>
> > I read this . I dont want to creat index. please just tell me how can I
> > add string to an integer matrix ?????????????????
>
> That is impossible in Matlab. Numeric matrices in Matlab can never contain
> character strings.
>
> >> from=[1 ;2 ;4; 6; 7; 8 ]
>
> Possibly what you are looking for is:
>
> from= num2str(from,'x%d')
> ans =
> x1
> x2
> x4
> x6
> x7
> x8

Tnx but it is not my answer.. I am not looking to display it in matlab.. I need to do computation on it.. This way give me a matrix (2 6) I need (1 6)
When I call 'from(1,1)' I need this answer 'x1' but here : from(1,1) = x and from(1,2)=1

Subject: how can I read text file in matlab ?

From: roya olyazadeh

Date: 4 May, 2010 02:57:04

Message: 23 of 27

"us " <us@neurol.unizh.ch> wrote in message <hrmfct$rhf$1@fred.mathworks.com>...
> "roya olyazadeh"
> > > 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 ?
>
> which ML version do you have(?)...
> can you upgrade(?)...
> note: the order of your character tags does not matter...
>
> us


I use matlab 7
I have another question.. Can you tell me how can I add string to a matrix.
For this example I have this matrix :

from=[1 ;2 ;4; 6; 7; 8 ]
Now I want to add x to them so I have
from=[x1;x2;x4;x6;x7;x8]
when I add them, Matlab gave me answers in numbers not in string like this
from=[x1;x2;x4;x6;x7;x8]

tnx againt for your help.

Subject: how can I read text file in matlab ?

From: Walter Roberson

Date: 4 May, 2010 03:25:59

Message: 24 of 27

roya olyazadeh wrote:

> Tnx but it is not my answer.. I am not looking to display it in matlab..
> I need to do computation on it.. This way give me a matrix (2 6) I
> need (1 6) When I call 'from(1,1)' I need this answer 'x1' but here :
> from(1,1) = x and from(1,2)=1

I believe you will have to write your own data class in order to
implement that. Matlab strings are arrays, and it is not possible with
the normal Matlab data types to store a character string in such a way
that the entire string can be referenced by an index in parenthesis.

Unless you create your own data class with its own subsref method, in
Matlab, from(1,1) cannot be an entire string.

If you are willing to modify your requirements slightly, then you might
find the following to be satisfactory:

from = cellstr(num2str(from,'x%d'));

After that, from(1,1) will NOT be 'x1', but it will be {'x1'} -- a 1 x 1
cell array that has inside it the string 'x1' . To get directly to that
string, you can code from{1,1} which will be 'x1' . Notice the use of {}
instead of () . Indexing with {} *can* give you an entire string back,
but indexing with () *cannot* give you an entire string back.

Subject: how can I read text file in matlab ?

From: roya olyazadeh

Date: 4 May, 2010 04:58:03

Message: 25 of 27


>
> If you are willing to modify your requirements slightly, then you might
> find the following to be satisfactory:
>
> from = cellstr(num2str(from,'x%d'));
>
> After that, from(1,1) will NOT be 'x1', but it will be {'x1'} -- a 1 x 1
> cell array that has inside it the string 'x1' . To get directly to that
> string, you can code from{1,1} which will be 'x1' . Notice the use of {}
> instead of () . Indexing with {} *can* give you an entire string back,
> but indexing with () *cannot* give you an entire string back.


Yes That 's true It worked . thank you .

syms a b c d
L01=((c-a)^2+(d-b)^2)^0.5;

FX=cellstr(num2str(from,'x%d'));
FY=cellstr(num2str(from,'y%d'));
TX=cellstr(num2str(to,'x%d'));
TY=cellstr(num2str(to,'y%d'));

for i= 1 : nd
L0(i)=subs(L01,[a b c d ],[FX(i,1) FY(i,1) TX(i,1) TY(i,1)]);
end
After this I will use 'diff' for example : diff(L0,x1)
and then do my computation LSE

Subject: how can I read text file in matlab ?

From: roya olyazadeh

Date: 5 May, 2010 06:53:04

Message: 26 of 27


>
> from = cellstr(num2str(from,'x%d'));
>
When I use 'cellstr' ,It is helpful for me for my function

FX=cellstr(num2str(from,'x%d'));
FY=cellstr(num2str(from,'y%d'));
TX=cellstr(num2str(to,'x%d'));
TY=cellstr(num2str(to,'y%d'));


for i= 1 : nd
L0(i)=subs(L01,[a b c d ],[FX(i,1) FY(i,1) TX(i,1) TY(i,1)]);
end


FX=
   'x1'
    'x2'
    'x3'
    'x4'
    'x5'
    'x6'

But when I want to use this way for 'diff(function,x1'
an error occurred :

for i= 1 : ns
AA(i,1)= diff(L0(i),FX(i));
end

??? Error using ==> sym.diff
Do not recognize argument number 2.



do you know what is the problem ?


When I defined it like this
FX=[x1 x2 x3 x4 x5 x6 ]


There is no error for diff(L0(i),FX(i));

Subject: how can I read text file in matlab ?

From: Walter Roberson

Date: 5 May, 2010 07:34:23

Message: 27 of 27

roya olyazadeh wrote:

> FX=cellstr(num2str(from,'x%d'));
> FY=cellstr(num2str(from,'y%d'));
> TX=cellstr(num2str(to,'x%d'));
> TY=cellstr(num2str(to,'y%d'));

> But when I want to use this way for 'diff(function,x1' an error occurred :
> for i= 1 : ns
> AA(i,1)= diff(L0(i),FX(i)); end
>
> ??? Error using ==> sym.diff
> Do not recognize argument number 2.

FX is a cell array of text, not of symbols. FX(i) is going to be a 1x1
cell array containing a text string. The first thing that I would try
would be to pass FX{i} to diff() instead of FX(i) -- using the {} will
pass the string instead of the cell array.

If that doesn't work, then I would use syms() to get the symbolic
variable associated with the name. For example, as a quick hack,

AA(i,1) = diff(L0(i), syms(FX{i}));

There would be more efficient ways to do this, but it is 2:30 in the
morning here and I am too tired to look them up in the reference
material (I do not have the symbolic toolbox to test against.)

Tags for this Thread

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.

Contact us