comp.lang.ada
 help / color / mirror / Atom feed
* is there Ada package similar to Matlab textscan?
@ 2015-07-17  3:52 Nasser M. Abbasi
  2015-07-17  4:25 ` tmoran
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Nasser M. Abbasi @ 2015-07-17  3:52 UTC (permalink / raw)


Matlab textscan() build-in function is very useful, I use it
all the time to read textual data from text files.

One bascially tells it the format of each field, and will load
all the data to Matlab data structure (called cells). Then
one can simply loop over the data and process it. The
format used is that of printf() standard format.

It is described here

http://www.mathworks.com/help/matlab/ref/textscan.html

One can also tell it how many lines to skip before
starting to read (this is in order to  bypass any
file headers) and tell it what is the field delimiter
between the columns of the data.

For example, given this text file

----- foo.txt---------------------
Student_ID  | Test1  | Test2  | Test3
    1,           91.5,     89.2,     77.3
    2,           88.0,     67.8,     91.0
    3,           76.3,     78.1,     92.5
--------------------------------

It is read as

C = textscan(fileID,'%d %f %f %f','HeaderLines',1,'Delimiter',',')

That is all. Now C will contain all the data in, in
what is called cell columns. C{1}, C{2}, C{3}, C{4}.

Is there a similar way to read data using Ada or must
one parse each field one by one themselves after
reading the file line by line?  I searched for an
Ada package that does something similar to textscan, but
so far did not find one.

thanks,
--Nasser






^ permalink raw reply	[flat|nested] 4+ messages in thread

* re: is there Ada package similar to Matlab textscan?
  2015-07-17  3:52 is there Ada package similar to Matlab textscan? Nasser M. Abbasi
@ 2015-07-17  4:25 ` tmoran
  2015-07-17  6:31 ` Dmitry A. Kazakov
  2015-07-17 11:05 ` Björn Lundin
  2 siblings, 0 replies; 4+ messages in thread
From: tmoran @ 2015-07-17  4:25 UTC (permalink / raw)


That's a CSV file.  I'm sure there are lots of folks' implementations.
The important part of mine is:

  function Field_Name(Csv_File: Csv_Files;
                      Index   : Field_Counts) return String;
  -- raises Constraint_Error if Index not in 1 .. Header's Field_Count

  function Field_Index(Csv_File:   Csv_Files;
                       Name: String) return Field_Counts;
  -- Ignoring case, find first field whose prefix = Name
  -- returns zero if not found

  procedure Read_A_Record(Csv_File: in out Csv_Files);
  -- May End_Error.
  -- If a record has fewer fields than the header indicated, it's
  -- padded out with null strings.

  function Field_Content(Csv_File: Csv_Files;
                         Index   : Field_Counts) return String;
  -- Raises Use_Error if called before any Read_A_Record.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: is there Ada package similar to Matlab textscan?
  2015-07-17  3:52 is there Ada package similar to Matlab textscan? Nasser M. Abbasi
  2015-07-17  4:25 ` tmoran
@ 2015-07-17  6:31 ` Dmitry A. Kazakov
  2015-07-17 11:05 ` Björn Lundin
  2 siblings, 0 replies; 4+ messages in thread
From: Dmitry A. Kazakov @ 2015-07-17  6:31 UTC (permalink / raw)


John Woodruff's Numeric_IO packages:

   http://www.dmitry-kazakov.de/ada/Numeric-Name-IO.htm

Download link:

   http://www.dmitry-kazakov.de/ada/Name_IO_Distribution-Nov10.zip

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: is there Ada package similar to Matlab textscan?
  2015-07-17  3:52 is there Ada package similar to Matlab textscan? Nasser M. Abbasi
  2015-07-17  4:25 ` tmoran
  2015-07-17  6:31 ` Dmitry A. Kazakov
@ 2015-07-17 11:05 ` Björn Lundin
  2 siblings, 0 replies; 4+ messages in thread
From: Björn Lundin @ 2015-07-17 11:05 UTC (permalink / raw)


On 2015-07-17 05:52, Nasser M. Abbasi wrote:


> one parse each field one by one themselves after
> reading the file line by line? 


Gnat.Awk is line based but parsing is simple:


with GNAT; use GNAT;
with GNAT.AWK;
with Text_io;
procedure Test is

  Computer_File : AWK.Session_Type;

begin
  AWK.Set_Current (Computer_File);
  AWK.Open (Separators => ",",
            Filename   => "path/to/file.dat");

  while not AWK.End_Of_File loop
    AWK.Get_Line;
    if AWK.Field(4)  = "GB" and then
       AWK.Field (18) = "PE"  then

       Text_io.Put_Line(AWK.Field (5) & " | " &
                        AWK.Field (8) & " | " &
                        AWK.Field (2) & " | " &
                        AWK.Field (11));
    end if;
  end loop;
  AWK.Close (Computer_File);

  see
<https://www2.adacore.com/gap-static/GNAT_Book/html/rts/g-awk__ads.htm>

for more examples

-- 
--
Björn


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-07-17 11:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-17  3:52 is there Ada package similar to Matlab textscan? Nasser M. Abbasi
2015-07-17  4:25 ` tmoran
2015-07-17  6:31 ` Dmitry A. Kazakov
2015-07-17 11:05 ` Björn Lundin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox