How can I capture/save my original CSV column headings in a .mat file?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

How can I capture/save my original CSV column headings in a .mat file?

George Farmer
I've looked around on the site but cannot find what I'm looking for.

CSV data is downloaded to Octave without the column/heading descriptors i.e. a pure data matrix results. What would be the most efficient approach in reading the original headings/column names in the csv file and name my imported data vectors/columns, subsequently saving the file as a .mat file? For example, would it be possible to pre-prepare a list of names and reference them, perhaps by way of a loop?

Be very grateful for suggestions, as I'm sure I'm not the first to encounter this. Many thanks.

Farmer
Reply | Threaded
Open this post in threaded view
|

Re: How can I capture/save my original CSV column headings in a .mat file?

bpabbott
Administrator
> On Apr 19, 2015, at 6:16 PM, Farmer <[hidden email]> wrote:

>
> I've looked around on the site but cannot find what I'm looking for.
>
> CSV data is downloaded to Octave without the column/heading descriptors i.e.
> a pure data matrix results. What would be the most efficient approach in
> reading the original headings/column names in the csv file and name my
> imported data vectors/columns, subsequently saving the file as a .mat file?
> For example, would it be possible to pre-prepare a list of names and
> reference them, perhaps by way of a loop?
>
> Be very grateful for suggestions, as I'm sure I'm not the first to encounter
> this. Many thanks.
>
> Farmer
I’ve written a script that does something very similar (csvload.m). It is attached.

You can use the script to load a csv-file into a structure whose field-names are taken from the column headers, and then save the data using the -struct option.

        s = csvload (your_csv_file, “returnstruct”, true, “headerlines”, 1);
        save your_mat_file.mat -struct s

The script does more than you require. So you may wish to modify it to suit your needs.

Ben





_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

csvload.m (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How can I capture/save my original CSV column headings in a .mat file?

George Farmer
Many thanks for that, appreciated.

I'm new to octave and just getting my head around coding. I'm not clear where I should place the line:
s = csvload (your_csv_file, “returnstruct”, true, “headerlines”, 1);

This is what I had for the function:
function [out, header] = csvload ('An2.csv', varargin), where 'An2.csv' was my comma delimited file.

Lots of error messages ensued.

Best,

Farmer


Contact number +48 781 415 432

On 20 April 2015 at 14:34, Ben Abbott <[hidden email]> wrote:
> On Apr 19, 2015, at 6:16 PM, Farmer <[hidden email]> wrote:
>
> I've looked around on the site but cannot find what I'm looking for.
>
> CSV data is downloaded to Octave without the column/heading descriptors i.e.
> a pure data matrix results. What would be the most efficient approach in
> reading the original headings/column names in the csv file and name my
> imported data vectors/columns, subsequently saving the file as a .mat file?
> For example, would it be possible to pre-prepare a list of names and
> reference them, perhaps by way of a loop?
>
> Be very grateful for suggestions, as I'm sure I'm not the first to encounter
> this. Many thanks.
>
> Farmer

I’ve written a script that does something very similar (csvload.m). It is attached.

You can use the script to load a csv-file into a structure whose field-names are taken from the column headers, and then save the data using the -struct option.

        s = csvload (your_csv_file, “returnstruct”, true, “headerlines”, 1);
        save your_mat_file.mat -struct s

The script does more than you require. So you may wish to modify it to suit your needs.

Ben







_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: How can I capture/save my original CSV column headings in a .mat file?

Philip Nienhuis
In reply to this post by bpabbott
bpabbott wrote
> On Apr 19, 2015, at 6:16 PM, Farmer <[hidden email]> wrote:
>
> I've looked around on the site but cannot find what I'm looking for.
>
> CSV data is downloaded to Octave without the column/heading descriptors i.e.
> a pure data matrix results. What would be the most efficient approach in
> reading the original headings/column names in the csv file and name my
> imported data vectors/columns, subsequently saving the file as a .mat file?
> For example, would it be possible to pre-prepare a list of names and
> reference them, perhaps by way of a loop?
>
> Be very grateful for suggestions, as I'm sure I'm not the first to encounter
> this. Many thanks.
>
> Farmer

I’ve written a script that does something very similar (csvload.m). It is attached.

You can use the script to load a csv-file into a structure whose field-names are taken from the column headers, and then save the data using the -struct option.

        s = csvload (your_csv_file, “returnstruct”, true, “headerlines”, 1);
        save your_mat_file.mat -struct s

The script does more than you require. So you may wish to modify it to suit your needs.
You could also check if csv2cell() in the io package can help you.

Philip
Reply | Threaded
Open this post in threaded view
|

Re: How can I capture/save my original CSV column headings in a .mat file?

George Farmer
Thanks. Will look into what this does.

George
Reply | Threaded
Open this post in threaded view
|

Re: How can I capture/save my original CSV column headings in a .mat file?

bpabbott
Administrator
In reply to this post by George Farmer

> On Apr 20, 2015, at 9:40 AM, George Matysiak <[hidden email]> wrote:
>
> Contact number +48 781 415 432
>
> On 20 April 2015 at 14:34, Ben Abbott <[hidden email]> wrote:
> > On Apr 19, 2015, at 6:16 PM, Farmer <[hidden email]> wrote:
> >
> > I've looked around on the site but cannot find what I'm looking for.
> >
> > CSV data is downloaded to Octave without the column/heading descriptors i.e.
> > a pure data matrix results. What would be the most efficient approach in
> > reading the original headings/column names in the csv file and name my
> > imported data vectors/columns, subsequently saving the file as a .mat file?
> > For example, would it be possible to pre-prepare a list of names and
> > reference them, perhaps by way of a loop?
> >
> > Be very grateful for suggestions, as I'm sure I'm not the first to encounter
> > this. Many thanks.
> >
> > Farmer
>
> I’ve written a script that does something very similar (csvload.m). It is attached.
>
> You can use the script to load a csv-file into a structure whose field-names are taken from the column headers, and then save the data using the -struct option.
>
>         s = csvload (your_csv_file, “returnstruct”, true, “headerlines”, 1);
>         save your_mat_file.mat -struct s
>
> The script does more than you require. So you may wish to modify it to suit your needs.
>
> Ben
>
>
> Many thanks for that, appreciated.
>
> I'm new to octave and just getting my head around coding. I'm not clear where I should place the line:
> s = csvload (your_csv_file, “returnstruct”, true, “headerlines”, 1);
>
> This is what I had for the function:
> function [out, header] = csvload ('An2.csv', varargin), where 'An2.csv' was my comma delimited file.
>
> Lots of error messages ensued

Don’t modify the csvload.m file until you are comfortable with programming Octave.

To start, just type the commands below at Octave’s prompt.

        s = csvload ("An2.csv", “returnstruct", true, “headerlines", 1);
        save foo.mat -struct s

Ben



_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: How can I capture/save my original CSV column headings in a .mat file?

George Farmer
Ben,

Excellent, it worked! Many thanks for your patience in dealing with this - very much appreciated.

George
Reply | Threaded
Open this post in threaded view
|

Re: How can I capture/save my original CSV column headings in a .mat file?

George Farmer
In reply to this post by bpabbott
Is it possible to write these two lines as a function()? Thanks.