octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to use textscan with gnu octave 3.6.2


From: Philip Nienhuis
Subject: Re: how to use textscan with gnu octave 3.6.2
Date: Fri, 11 Jan 2013 06:46:58 -0800 (PST)

brico wrote
> hello :)
> 
> i am using gnu octave 3.6.2 since  10 days and  i ned to read all the rows
> of a (.txt)file with textscan.
> the file i want to read looks like
> "
> 4 = Interface Serial-No :
> 5 = Calibrated by :
> 6 = Radiation Quality :
> 7 = Measuring Quantity :
> 
> [Kanal_Info]
> ;Kanal-Nr. =
> Status(Aktiv/Inaktiv);A/D-Kanal(1-4);IF-KanalNr;X_0;Y_0;K-Faktor;"Bemerkung"
> 
> 0 = Aktiv;1;0;-126;0;1.021;""
> 1 = Aktiv;1;1;-123;0;1.021;""
> 2 = Aktiv;1;2;-120;0;1.021;""
> 3 = Aktiv;1;3;-117;0;1.021;""
> 4 = Aktiv;1;4;-114;0;1.021;""
> 5 = Aktiv;1;5;-111;0;1.021;""
> 6 = Aktiv;1;6;-108;0;1.021;""
> 7 = Aktiv;1;7;-105;0;1.021;""
> 8 = Aktiv;1;8;-102;0;1.021;""
> 9 = Aktiv;1;9;-99;0;1.021;""
> 10 = Aktiv;1;10;-96;0;1.021;""  
> 
> and the code i am using is the following.
> "
> function error_code = Test2
> fid2 = fopen('C:\Users\brico\Desktop\slow.txt');
> txt = '';
> while ~strcmp(txt, ';Kanal-Nr. =
> Status(Aktiv/Inaktiv);A/D-Kanal(1-4);IF-KanalNr;X_0;Y_0;K-Faktor;"Bemerkung"')
> txt = fgetl(fid2);
> end
> B = textscan(fid2, '%f = %s%f%f%f%f%f%s', 'delimiter', ';',
> 'MultipleDelimsAsOne', 1);
> fclose(fid2);
> x_array_in_mm = B{5};                                                         
>  
> y_array_in_mm = B{6};
> property      = B{2};
> AD_kanal      = B{3};
> IF_kanal      = B{4};
> Bemerkung     = B{8};
> x_dist_in_mm = 10;
> y_dist_in_mm = 10;
> num_pos = length(x_array_in_mm);
> result =  -ones(num_pos, 1);
> x_array_in_mm
> y_array_in_mm
> property
> ..
> Wenn i start this code on the comand window , it returns somethings else 

>  . I did a sreenshot of theresult and you can find it as 
> image_attachment.
<http://octave.1599824.n4.nabble.com/file/n4648766/screenshot_octave.jpg> 
> 
> please can any one help me 

>  ?
> i will like to read everything on this file includet the 'aktive' and ' ""
> ' because i will need them later.
> i have  ones used this code in Matlab and it has function.
> thanks for your Help 

> Brico

Trying your example file with:

octave-3.6.2.exe:10> fid = fopen ('tst.fil');
octave-3.6.2.exe:11> B = textscan (fid, "%f = %s%f%f%f%f%f%s", "delimiter",
";", "headerlines", 9)

I get:

B =
{
  [1,1] =

      0
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10

  [1,2] =
  {
    [1,1] =  Aktiv
    [2,1] =  Aktiv
    [3,1] =  Aktiv
    [4,1] =  Aktiv
    [5,1] =  Aktiv
    [6,1] =  Aktiv
    [7,1] =  Aktiv
    [8,1] =  Aktiv
    [9,1] =  Aktiv
    [10,1] =  Aktiv
    [11,1] =  Aktiv
  }
  [1,3] =

     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1

  [1,4] =

      0
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10

  [1,5] =

    -126
    -123
    -120
    -117
    -114
    -111
    -108
    -105
    -102
     -99
     -96

  [1,6] =

     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0

  [1,7] =

     1.0210
     1.0210
     1.0210
     1.0210
     1.0210
     1.0210
     1.0210
     1.0210
     1.0210
     1.0210
     1.0210

  [1,8] =
  {
    [1,1] = ""
    [2,1] = ""
    [3,1] = ""
    [4,1] = ""
    [5,1] = ""
    [6,1] = ""
    [7,1] = ""
    [8,1] = ""
    [9,1] = ""
    [10,1] = ""
    [11,1] = ""
  }
}

I wonder why the whole stanza with fgetl is needed - does the number of
headerlines vary between files?

You may need to fiddle a bit as I don't know the exact number of headerlines
- is the first single '"' on the topmost line part of the file or just a
quote delimiter? (in the latter case you forgot the closing one.)

Note that you cannot read the ";Kanal = ...... etc line with one and the
same textscan run, because that line has non-numeric fields that don't match
the rest of the data. AFAICS it'll be eaten anyway by your fgetl stanza
before the actual textscan invocation.

BTW I see no need for a "MultipleDelimsAsOne" parameter - do you really need
it? Octave's textscan is not as robust as Matlab's one, it'll get seriously
confused if the number of data on each line varies between lines (which may
happen if you have one or more adjacent ";" characters).

Philip




--
View this message in context: 
http://octave.1599824.n4.nabble.com/how-to-use-textscan-with-gnu-octave-3-6-2-tp4648766p4648773.html
Sent from the Octave - Maintainers mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]