WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

how to use textscan with gnu octave 3.6.2

 

Brico
Forum-Newbie

Forum-Newbie


Beiträge: 6
Anmeldedatum: 10.01.13
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 10.01.2013, 19:02     Titel: how to use textscan with gnu octave 3.6.2
  Antworten mit Zitat      
how to use textscan with octave

hello Smile

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.

"
Code:
 
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 the
result and you can find it as image_attachment.

please can any one help me ?
i have ones used this code in Matlab and it has function.
thanks for your Help
Brico

screenshot_octave.jpg
 Beschreibung:
my results

Download
 Dateiname:  screenshot_octave.jpg
 Dateigröße:  39.7 KB
 Heruntergeladen:  1957 mal
Private Nachricht senden Benutzer-Profile anzeigen


markuman
Forum-Guru

Forum-Guru


Beiträge: 320
Anmeldedatum: 14.12.12
Wohnort: ---
Version: 2013a/2013b, Octave 3.6.4, 3.7.7
     Beitrag Verfasst am: 10.01.2013, 19:23     Titel:
  Antworten mit Zitat      
i guess = is a string too.
try

Code:


B = textscan(fid2, '%f %s %s%f%f%f%f%f%s', 'delimiter', ';', 'MultipleDelimsAsOne', 1);

 


but then i guess your b{numbering} doesn't match anymore.
_________________

DIY OR DIE Cool

entropie=char(floor(94*rand(1, round(100.*rand)) + 32))
https://github.com/markuman
Private Nachricht senden Benutzer-Profile anzeigen
 
markuman
Forum-Guru

Forum-Guru


Beiträge: 320
Anmeldedatum: 14.12.12
Wohnort: ---
Version: 2013a/2013b, Octave 3.6.4, 3.7.7
     Beitrag Verfasst am: 10.01.2013, 19:30     Titel:
  Antworten mit Zitat      
if your file looks like that:

Code:

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;""
 

.... 8 lines before your values began
try with this lines function that

Code:

octave:4> dlmread ('einlesen.txt' ,';',[8,1,lines('einlesen.txt'),5])
ans =

     1.00000     0.00000  -126.00000     0.00000     1.02100
     1.00000     1.00000  -123.00000     0.00000     1.02100
     1.00000     2.00000  -120.00000     0.00000     1.02100
     1.00000     3.00000  -117.00000     0.00000     1.02100
     1.00000     4.00000  -114.00000     0.00000     1.02100
     1.00000     5.00000  -111.00000     0.00000     1.02100
     1.00000     6.00000  -108.00000     0.00000     1.02100
     1.00000     7.00000  -105.00000     0.00000     1.02100
     1.00000     8.00000  -102.00000     0.00000     1.02100
     1.00000     9.00000   -99.00000     0.00000     1.02100
     1.00000    10.00000   -96.00000     0.00000     1.02100

 

_________________

DIY OR DIE Cool

entropie=char(floor(94*rand(1, round(100.*rand)) + 32))
https://github.com/markuman
Private Nachricht senden Benutzer-Profile anzeigen
 
Brico
Themenstarter

Forum-Newbie

Forum-Newbie


Beiträge: 6
Anmeldedatum: 10.01.13
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 10.01.2013, 20:31     Titel: how to use textscan with gnu octave 3.6.2
  Antworten mit Zitat      
hello,
thanks for your fast response but i do not really understand your code Sad
please can you explain to me, what 'dlmread ' does here? and were can i insert it in my code?
i just read the 'Help dlmread ' but i could not understand what you did.
is 5 the number of colum?
do you mean i can use the ' lines function' instead of 'fgetl'?
excuse me if my question are not clear but like i just said i am begenning with Octave and i am just trying to understand
thanks for your help Smile
brico
Private Nachricht senden Benutzer-Profile anzeigen
 
markuman
Forum-Guru

Forum-Guru


Beiträge: 320
Anmeldedatum: 14.12.12
Wohnort: ---
Version: 2013a/2013b, Octave 3.6.4, 3.7.7
     Beitrag Verfasst am: 10.01.2013, 20:55     Titel:
  Antworten mit Zitat      
Code:
[8,1,lines('einlesen.txt'),5]


The 8 is skipping the first 8 lines. That's what you're doing with your while strgcmp..
The 1 skipp column 1. If you realy need it, creat it like test=[0:lines('yourfile')]';
The following two numbers define the range to read (calculated starts at your skipped rows and columns (the first two option nr.)) . Again, first row, and second column.
You have to save my lines.m function in the same folder. It just counts the lines of a file (so it reply a numbr. and usualy you don't know exactly how many lines/measurements you've got in a file Wink it did the job). You can say, it set the column range to the end of the file.
and the range of column to 5, because the last column "" you don't need too i guess.

but be aware, this dlmread command won't work in matlab when you try to skip non numerical expressions like everywhere in your file Very Happy

it can replace your complete fopen to fclose part, if this is enough for you.
you only have to customize your variablenames (x_array_in_mm, y_array_in_mm ....) to the coloumns of the dlmread output.

as i see, you are asking on the octave mailing lists too. There isn't just one way of doing it. It was just a suggestion.

i hope you can follow my worse english.
take care, markus
_________________

DIY OR DIE Cool

entropie=char(floor(94*rand(1, round(100.*rand)) + 32))
https://github.com/markuman
Private Nachricht senden Benutzer-Profile anzeigen
 
Brico
Themenstarter

Forum-Newbie

Forum-Newbie


Beiträge: 6
Anmeldedatum: 10.01.13
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 11.01.2013, 12:07     Titel:
  Antworten mit Zitat      
hallo Marcus
Thank you for your reply.
Is there another way of doing this without the lines? The reason why I say that is the following:
The data I am trying to read is a very long file it has more than 50 line before the value begins (that’s the reason why I use strcmp). The values go from 0 till 600. You can fine an example as file-attachment. ('neue Textdokument.txt')
I need the 5th and the 6th Colum (my x- and y-coordinate) of this file to do a bilinear interpolation and i am supposed to write my results in a (.txt) file.
i do it like
Code:

for row_id = 1:num_pos
      fprintf(fid4, '%d = %s;%d;%d;%3f;%3f;%3f;%s\r\n',...
                  row_id-1, property{row_id}, AD_kanal(row_id), IF_kanal(row_id), x_array_in_mm(row_id), y_array_in_mm(row_id), result(row_id), Bemerkungen{row_id});
 end % with num_pos = length(x_array_in_mm);
 

You can also find an example of my results as file-attachment.
As you will see my must have this specific format:
Code:

0 = Aktiv;1;0;-126.000000;0.000000;0.893600;""
1 = Aktiv;1;1;-123.000000;0.000000;0.903800;""

That means I need to read all everything on this file because i will need them later to write my result-file.txt.
I will be very grateful if you can show me how to solve this with dlmread or everything else.... since texscan doesn’t function well Smile
i can send you my interpolation code if you are interested in it.

Sorry for the long message.

vielen Dank!
brice

Neu Textdokument.txt
 Beschreibung:
my results

Download
 Dateiname:  Neu Textdokument.txt
 Dateigröße:  31.26 KB
 Heruntergeladen:  1403 mal
Neu Textdokument (2).txt
 Beschreibung:
this is the data were i getv my coordinates. (modified)

Download
 Dateiname:  Neu Textdokument (2).txt
 Dateigröße:  3.8 KB
 Heruntergeladen:  1242 mal
Private Nachricht senden Benutzer-Profile anzeigen
 
markuman
Forum-Guru

Forum-Guru


Beiträge: 320
Anmeldedatum: 14.12.12
Wohnort: ---
Version: 2013a/2013b, Octave 3.6.4, 3.7.7
     Beitrag Verfasst am: 11.01.2013, 18:44     Titel:
  Antworten mit Zitat      
Brico hat Folgendes geschrieben:

The data I am trying to read is a very long file


That's exactly for what lines() is made for Very Happy

Brico hat Folgendes geschrieben:
it has more than 50 line before the value begins (that’s the reason why I use strcmp).


so just pimp the lines function, that it will give you the line for start reading (your while loop), and the end of the file, where it should stop reading.

build it like [from, to]=yourasskickingfunction('yourchaoticfile');
And than read it with data=dlmread('yourchaoticfile',' ',[from,1,5,to]);


Wir können sonst auch auf Deutsch weiterreden wenn es keinen hier verstört Cool
Weil so ganz erschließt sich mir dein problem nicht, weil der dlmread befehl in verbindung mit der lines funktion doch alle Daten einliest.
_________________

DIY OR DIE Cool

entropie=char(floor(94*rand(1, round(100.*rand)) + 32))
https://github.com/markuman
Private Nachricht senden Benutzer-Profile anzeigen
 
markuman
Forum-Guru

Forum-Guru


Beiträge: 320
Anmeldedatum: 14.12.12
Wohnort: ---
Version: 2013a/2013b, Octave 3.6.4, 3.7.7
     Beitrag Verfasst am: 11.01.2013, 19:20     Titel:
  Antworten mit Zitat      
Okey, in der Theorie nimmst du diese Funktion

Code:

function [from, to] = fromto(filename)
fid = fopen(filename,'r');
lines=0;
  if fid < 1
    from = 0;
    to = 0;
  else
    from = 0;
    to = 0;
    while 1
      linestmp = fgetl(fid);
   if strcmp(linestmp, ';Kanal-Nr. = Status(Aktiv/Inaktiv);A/D-Kanal(1-4);IF-KanalNr;X_0;Y_0;K-Faktor;"Bemerkung"')
      from=lines+2; % +2 weil +1 direkt diese zeile ist (lines wird erst nach den if! wie aber erst nach dieser zeile lesen wollen!
   end;

        if ~ischar(linestmp)
      to=lines;
                break;
        end;
      lines = lines + 1;
    end;
    fclose(fid);
end;
 


Du verwendest sie so:

Code:
octave:38> [from,to]=fromto('Neu Textdokument.txt' );
octave:39> data=dlmread('test.txt' ,';',[from,1,to,5]);
 


Allerdings fängt er zu spät an zu lesen, was vermutlich daran liegt, dass dlmread eine Leerzeile nicht als Zeile betrachtet.
Vielleicht sieht aber unter Windows die Datei nicht so Kaputt aus http://paste.osuv.de/index.php/epE/

Und wenn doch, musst du halt ein Korrekturschlag für den from Wert machen.
Code:

 data=dlmread('Neu Textdokument.txt' ,';',[round((from/2)+2),1,to,5]);
 


Oder wir reden immer noch aneinander vorbei Smile
_________________

DIY OR DIE Cool

entropie=char(floor(94*rand(1, round(100.*rand)) + 32))
https://github.com/markuman
Private Nachricht senden Benutzer-Profile anzeigen
 
Brico
Themenstarter

Forum-Newbie

Forum-Newbie


Beiträge: 6
Anmeldedatum: 10.01.13
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 13.01.2013, 18:02     Titel:
  Antworten mit Zitat      
hi marcus
i've tried your example and it functions Smile thanks again.
someone in the Octave-group also show me how i can do this with textscan:
Code:
B = textscan(fid2, '%f = %s%f%f%f%f%f%s', 'delimiter', ';', 'headerlines', 1);

in case you are intrested in it Wink
Know i need this result to do a bilinear interpolation. The code i wrote is the following

Code:
result =  -ones(num_pos, 1);
 if strcmp(property(row_id), 'Aktiv') == 1
             result(row_id, 1) = (1 - r) * (1 - s) * kf_mat(x0, y0) + ...
                                 (1 - r) *      s  * kf_mat(x0, y01) + ...
                                      r  * (1 - s) * kf_mat(x01, y0) + ...
                                      r  *      s  * kf_mat(x01, y01);
           else
                    result(row_id) = -2;
           endif
result
 

Octave should do a bilinear interpolation just when the canal is 'Aktiv' . but i think the command strcmp doesn't function this way. on the command-window i get just "-2 " everywhere. When i write the code this way,

Code:
result(row_id, 1) = (1 - r) * (1 - s) * kf_mat(x0, y0) + ...
                                 (1 - r) *      s  * kf_mat(x0, y01) + ...
                                      r  * (1 - s) * kf_mat(x01, y0) + ...
                                      r  *      s  * kf_mat(x01, y01);

the command -window gives me the right results.

Please do you have an idea how i can use strcmp in this case?
This is the complete code if you need it

ich würde so gerne auf Deutsch weiterschreiben aber ich glaube Englisch zu üben ist auch eine gute sache Smile
thanks for your Help
brice

Interpolate5.m
 Beschreibung:
my code

Download
 Dateiname:  Interpolate5.m
 Dateigröße:  4.38 KB
 Heruntergeladen:  1188 mal
Private Nachricht senden Benutzer-Profile anzeigen
 
Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

Du kannst Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum antworten.
Du kannst deine Beiträge in diesem Forum nicht bearbeiten.
Du kannst deine Beiträge in diesem Forum nicht löschen.
Du kannst an Umfragen in diesem Forum nicht mitmachen.
Du kannst Dateien in diesem Forum posten
Du kannst Dateien in diesem Forum herunterladen
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2024 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.