how to import CSV file (problem with wrong written file)

2 views (last 30 days)
Hello everyone!
I am using sensor CHR UM7 from CH Robotics, which can maklogs of data. Unfortunately it safe this in CSV file but the type of record is wrong.. why?
comma divide numbers from number but in my file I have:
0,12334,0,0323233 (look in atteched file).
In fact these are not for numbers but two numbers (but with comma). Matlab can recognise this unfrtunately as a 4 numbers..
Does anyone of you has any ideas how to solve this problem?
  5 Comments
Stephen23
Stephen23 on 30 Jun 2017
Edited: Stephen23 on 30 Jun 2017
@Matthew Jameson: are the files created on a computer which is set to use the decimal comma (e.g. most non-english European languages)? It is possible that the problem is caused by a hard-coded comma being used as the field separator, together with the computers locale setting used to generate the numeric strings.
If so then the whole problem could be easily avoided by switching the computer's locale settings to use the decimal point instead of the decimal comma.
Matthew Jameson
Matthew Jameson on 5 Jul 2017
Thank you very, very much! That was a language bug in Windows 7! After changing local settings on USA everything works fine!
Thank you so much! It was impossible to solve this problem without your knowledge! :)

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 28 Jun 2017
One approach:
D = xlsread('blaa.csv');
Dc = sprintf('%.0f.%.0f\t%.0f.%.0f\t%.0f.%.0f\t%.0f.%.0f\n', D'); % Formatted Print
Dt = textscan(Dc, '%f%f%f%f', 'Delimiter','\t'); % Read
Dm = cell2mat(Dt); % Convert To Matrix (Output)
This is not as efficient as I would like it to be. It does however have the advantage of working.
  2 Comments
Matthew Jameson
Matthew Jameson on 28 Jun 2017
Thanks for you quick response.
I have turned on this script, but matrixes are empty.
Star Strider
Star Strider on 28 Jun 2017
Edited: Star Strider on 29 Jun 2017
My code worked with the file you posted in R2017a.
I have attached ‘Dm’ as a ‘.mat’ file to demonstrate that my code works with your file.
What version of MATLAB are you using?

Sign in to comment.

More Answers (2)

per isakson
per isakson on 30 Jun 2017
Edited: per isakson on 30 Jun 2017
I've done an experiment with regular expression on R2016a. I encountered a problem. (?m), 'lineanchors' doesn't work as documented for a string, in which CRLF is used as new line character. It's works with LF as new line character. That's confusing. My solution is to replace fileread by txt2str , which returns a string with LF as new line character.
str = txt2str('blaa.csv'); % read file to string. Ensure LF is used for newline.
xpr = [ '(?m)' ... match the '^' and '$' at head and tail of a line
, '^' ... beginning of line
, '([+-]?\d+)' ... group dec part of 1st number
, ',' ... decimal separator
, '(\d+,[+-]?\d+)' ... group frac part of 1st number, list sep
... and decimal part of 2nd number
, ',' ... decimal separator
, '(\d+,[+-]?\d+)' ... group frac part of 2nd number, list sep
... and decimal part of 3rd number
, ',' ... decimal separator
, '(\d+,[+-]?\d+)' ... group frac part of 3rd number, list sep
... and decimal part of 4th number
, ',' ... decimal separator
, '(\d+)' ... group frac part of 3rd number, list sep
, '[ ]*$' ... trailing blanks and end of line
];
buf = regexprep( str, xpr, '$1.$2.$3.$4.$5' ); % insert decimal points
%
cac = textscan( buf, '%f%f%f%f', 'Delimiter',',', 'CollectOutput',true, 'Headerlines',1 );
num = cac{1,1};
inspect the result
>> num(1:3,:)
ans =
12.9086 -0.0277 -0.0320 -0.8833
12.9668 -0.0275 -0.0316 -0.8838
13.0281 -0.0270 -0.0315 -0.8842
>> whos num
Name Size Bytes Class Attributes
num 234x4 7488 double
  2 Comments
Walter Roberson
Walter Roberson on 30 Jun 2017
per:
fileread() and then delete the CR would work.
It is true that lineanchors only expect newline. But you could use '\r?$' or '(?=\r?)$'
per isakson
per isakson on 30 Jun 2017
Edited: per isakson on 30 Jun 2017
Walter
Yes and yes, but
  • "delete the CR" adds clutter to the code and takes a bit of time, since the size of the string changes.
  • "use '\r?$' or '(?=\r?)$'" adds clutter
"It is true that lineanchors only expect newline" Yes, that's true. And the same is true with 'dotexceptnewline'. IMO: this is a design mistake, since
  • Matlab users typically don't want to bother about new line characters.
  • PCRE (e.g. https://regex101.com/) handles CRLF and LF the same way (in these cases).
  • I cannot think of any situation, in which the Matlab way offers an advantage.
The difference between txt2str and fileread is
[fid, msg] = fopen( filespec, 'rt' );
in place of
[fid, msg] = fopen(filename);

Sign in to comment.


Matthew Jameson
Matthew Jameson on 30 Jun 2017
THANK YOU ALL SO MUCH FOR YOUR HELP!
I will test this and let you know.
I am so happy that people here are so helpful and so clever in matlab issues.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!