Issue
I have an .csv
file made in Excel and that Excel uses ;
as delimiter. I don't like that, but I cannot change it.
So I have to accept it.
I want to read that .csv
file in GNU Octave
, but I can't really do it because the float numbers inside the .csv
file is separated with ,
e.g 15,25
and not separated with .
e.g 15.25
because in Excel, it assumes that 15.25
is a text string and 15,25
is a number. Yes, I know, it's weird.
If I change all ,
to .
in my .csv
file, then I can read the .csv
file with dlmread
or csvread
. But in this case, I don't want to change ,
to .
because that's a standard Excel configuration.
So my question is:
If you have a .csv
file were the float numbers are displayed with ,
. How can you read that .csv
file in GNU Octave
then?
Solution
For a much more flexible csv reader than the default matlab-compatible csvread
and dlmread
, use the csv2cell
function from the io
pkg. If you haven't used packages before, this is how you would do that:
pkg install io -forge % install io package if you haven't already
pkg load io % load it
Then use csv2cell to read the values as strings into a cell (declaring ;
as the desired delimiter); then use strrep
to replace ,
to .
in your strings; and then finally use str2double
to convert those strings to numbers.
You can do this with the following one-liner:
str2double( strrep( csv2cell( 'testo.csv', ';' ), ',', '.' ) )
You could also wrap this into an anonymous function:
read_my_csv = @( myfile ) str2double( strrep( csv2cell( myfile, ';' ), ',', '.' ) );
Data = read_my_csv( 'data.csv' );
PS. Tux the penguin added for extra perfection, as requested.
_nnnn_
dGGGGMMb ,"""""""""""""".
@p~qp~~qMb | Linux Rules! |
M|@||@) M| _;..............'
@,----.JM| -'
JS^\__/ qKL
dZP qKRb
dZP qKKb
fZP SMMb
HZM MMMM
FqM MMMM
__| ". |\dS"qML
| `. | `' \Zq
_) \.___.,| .'
\____ )MMMMMM| .'
`-' `--'
(source: https://www.asciiart.eu/computers/linux)
Answered By - Tasos Papastylianou Answer Checked By - Pedro (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.