help-octave
[Top][All Lists]
Advanced

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

Re: fscanf


From: Thomas Walter
Subject: Re: fscanf
Date: Wed, 14 Apr 1999 11:21:55 +0200

>>>>> "Joao" == Joao Cardoso <address@hidden> writes:

    Joao> Thomas Walter wrote:
    >> 
    >> Hello,
    >> is it only me who has problems with 'fscanf' and friends?
    >> 
    >> I have measured data in a data file written as ASCII, i.e.:
    >> 10  123.4
    >> 20  234.5
    >> 30  345.6
    >> 40  456.7
    >> 50  567.8
    >> 60  678.9
    >> 
    >> This is data in one line belongs together.  In this case I have 2
    >> columns and the number of lines/rows is unknown, bu defined by EOF.
    >> If I read the data with this knowledge I always jump into the same
    >> pit.  The 'fscanf' function wants lines and colomuns in the opposited
    >> order.
    >> 
    >> Apart from that the matrix I get has the size 2x6, this means:
    >> 10 20 30 40 50 60
    >> 123.4 234.5 345.6 456.7 567.8 678.9
    >> 
    >> Is this behaviour only strange for me?

    Joao> Could you include your fscanf command? That's hard to figure out what 
is
    Joao> happening without it... have you read the online help about fscanf?

Yes I read it, at least 5 times.

I have investigated more time on this problem and finally I have two
solutions.  Suggestions and ideas are welcome!

The first solution uses 'fscanf' to read the data and the second uses
the 'load' command.  If you want to play with the two functions, the
sources are at the end. Put the data from above (the first data set!!!)
into a test file 'tr_test.dat'.  You can use any name but NO paths.
The test file must be in the current directory.

The second example looks like a hack, I think, but it is hard to use
'load' with the filename to use in a string variable.

Bye
Thomas

======= start data file 'tr_test.dat' ========
10  123.4
20  234.5
30  345.6
40  456.7
50  567.8
60  678.9
======= end data file 'tr_test.dat' ========

========= start first function using 'fscanf' ============
function [m, m_n] = read_resist (infid, data_per_line)
  ## Read a ASCII-file containing measured resistance
  ## The format of the file:
  ## Lines starting with a '#' are comments, can be anywhere
  ## Datalines have the format:  time_in_seconds  resistance
  ##
  ## !!!! This version forbids comments in the data !!!!
  ## You must do something like
  ## infid=popen("drop_comment r_ag3_12.mes", "r");[m, m_n]=fscanf (infid, 
"%g",[2,Inf]);fclose(infid);
  ##
  ## Usage:
  ##       [m, m_n] = read_resist (infid, data_per_line)
  ##
  ## Input:
  ##  infid: file descriptor to read from
  ##
  ## Output:
  ##  m : matrix with the complete data set.
  ##  m_n : total number of read values.

  ## Copyright (c) 1999  Thomas Walter
  ## $Id: read_resist.m,v 1.2 1999/04/13 14:11:12 twalter Exp $

  ## Sanity checks
  if ((nargin != 2) || (nargout != 2))
    usage ("[m, m_n] = read_resist (infid, data_per_line)");
  endif
  
  [m, m_n] = fscanf (infid, "%g", [data_per_line, Inf]);
  m = m';
  
endfunction
========= end first function using 'fscanf' ============

========= start second function using 'load' ============
function [time_s, resistance] = read_resist_file (filename)
  ## Read a ASCII-file containing measured resistance
  ## The format of the file:
  ## Lines starting with a '#' are comments, can be anywhere
  ## Datalines have the format:  time_in_seconds  resistance
  ##
  ## Usage:
  ##       [time_s, resistance] = read_resist_file (filename)
  ##
  ## Input:
  ##  filename: file name to open, MUST not have a path !!!!
  ##
  ## Output:
  ##  time_s : time in seconds
  ##  resistance : the value of the resistance unit 'OHM'

  ## Copyright (c) 1999  Thomas Walter
  ## $Id: read_resist_file.m,v 1.3 1999/04/13 14:04:50 twalter Exp $

  ## Sanity checks
  if ((nargin != 1) || (nargout != 2))
    usage ("[time_s, resistance] = read_resist_file (filename)");
  endif
  if (!isstr (filename))
    usage ("read_resist_file (filename);  with filename is a string!");
  endif

  ## load the file with the name given by string variable filename. I.E.: 
filename = "foo.dat"
  load (filename);

  ## Now the matrix/variable 'foo' exists, but cannot simply accessed
  parts = split (filename, "."); # drop extension
  base = parts (1,:);            # now I have the variable name
  ## But still not accessible here
  default_eval_print_flag_save = default_eval_print_flag;
  unwind_protect
    default_eval_print_flag = 0;        # shut up
    m = eval (base);
  unwind_protect_cleanup
    default_eval_print_flag = default_eval_print_flag_save;
  end_unwind_protect
  ## I finally can access it with the local name 'm'
  [nr, nc] = size (m);
  time_s = m (:, 1);
  resistance = m (:, 2);

endfunction
========= end second function using 'load' ============



-- 

Platzangst:
Der dauerhaft Zustand eines Luftballons.

----------------------------------------------
Dipl. Phys. Thomas Walter
Inst. f. Physiklische Chemie II
Egerlandstr. 3                          Tel.: ++9131-85 27326 / 27330
91058 Erlangen, Germany                 email: address@hidden



reply via email to

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