help-octave
[Top][All Lists]
Advanced

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

Re: Read a CSV file and plot a graph


From: PhilipNienhuis
Subject: Re: Read a CSV file and plot a graph
Date: Mon, 4 Jun 2018 12:06:54 -0700 (MST)

yamane wrote
> Hi,
> 
> Please, is there an way to read a CSV file and split it to plot 2
> graphics?
> 
> ===== This is the conntent of the CSV file =====
> Graph A
> displacement,weight
> 0, 0
> 1, 20
> 2, 40
> 3, 60
> 4, 80
> 5, 100
> Graph B
> displacement,weight
> 0, 0
> 1, 15
> 2, 30
> 3, 45
> 4, 60
> 5, 75
> ==================================================
> 
> I looking for a way to make 2 graphs (A e B) with the datas that is
> available in a single file...

With textscan you can easily read the separate file sections:

>> fid = fopen ('multicsv.csv', 'r')  ## open file read-only just to be sure
fid =  3
>> C = cell2mat (textscan (fid, '%f %f', 'delimiter', ',', 'headerlines',
>> 2))
C =
     0     0
     1    20
     2    40
     3    60
     4    80
     5   100

>> D = cell2mat (textscan (fid, '%f %f', 'delimiter', ',', 'headerlines',
>> 2))
D =
    0    0
    1   15
    2   30
    3   45
    4   60
    5   75

>> fclose (fid);
>>

When reading from file, textscan stops at the first error and retains the
file pointer. That happens when the %f format specifier hits the second set
of headerlines.  cell2mat() is used to convert textscan's output into a
numerical array. You can also do that separately.
>From there you can repeat the textscan statement with another output
argument, and it starts at the retained file pointer until the next set of
headerlines.
With some ingenuity you can even do this in a for loop, or in a while loop
to read an arbitrary number of file sections. Be careful as each file
section must have the same number of headerlines.
Don't forget to close the file handle afterwards with fclose ().

Once there, making the graphs is an easy exercise.

HTH

Philip



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



reply via email to

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