Read a CSV file and plot a graph

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Read a CSV file and plot a graph

yamane
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...

Thank you in advance,
Renato


Reply | Threaded
Open this post in threaded view
|

Re: Read a CSV file and plot a graph

Ron.Simonson
On 06/04/2018 07:18 AM, Renato S. 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...
Perhaps this will get you started.  I generally put a '#' comment
character at the begging of comment lines as shown in the example
data file.  Then it is easy to use the .m file shown here to do
something close to what you are asking for.  There are certainly
methods within octave to read files line by line and decipher what
each line contains but the attached is simple, and quick so this
is what I will often do.

Others will undoubtedly provide you with much better solutions
but hopefully this will get you started.

Talk to you later.  Ron.



mydata.text (184 bytes) Download Attachment
rceprog.m (249 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Read a CSV file and plot a graph

yamane
2018-06-04 18:08 GMT+02:00 Ron.Simonson <[hidden email]>:

> On 06/04/2018 07:18 AM, Renato S. Yamane wrote:
>> 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...
>
>
> Perhaps this will get you started.  I generally put a '#' comment
> character at the begging of comment lines as shown in the example
> data file.  Then it is easy to use the .m file shown here to do
> something close to what you are asking for.  There are certainly
> methods within octave to read files line by line and decipher what
> each line contains but the attached is simple, and quick so this
> is what I will often do.
>
> Others will undoubtedly provide you with much better solutions
> but hopefully this will get you started.


Very interesting way to skip the headers!!
Thank you for this amazing tip...

Regards,
Renato


Reply | Threaded
Open this post in threaded view
|

Re: Read a CSV file and plot a graph

PhilipNienhuis
In reply to this post by yamane
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