4

My problem is, that I've got CSV-data of the following format:

1,000333e+003;6,620171e+001
1,001297e+003;6,519699e+001
1,002261e+003;6,444984e+001

I want to read the data into matlab, but csvread requires it to be comma separated, and I have not been able to find a solution to the comma-decimal mark. I guess I can use textscan in some way?

I'm sorry to ask such an (I think) easy question, but I hope someone can help. None of the other questions/answers in here seems to be dealing with this combination of comma and semicolon.

Niels Møller
  • 203
  • 1
  • 3
  • 9
  • If the "import data" button works, try `importdata` as a function . I also think this might depend on your locale. e.g. if you do `str2num('1,000333e+003')` at the command line, does it convert correctly?. – nkjt Apr 27 '15 at 14:19

2 Answers2

10

EDIT3 (ACCEPTED ANSWER): Using the import data button in the variable section of the home toolbar it is possible to customise how the data is imported. once that is done you can click import selection beneath the arrow and generate a script or function that will follow the same rules defined in the import data window.

import Data Instructions

--------------------------------------------------kept for reference--------------------------------------------------

You can use dlmread it works in the following format

M = dlmread(filename,';')

the filename is a string with the full path of the file unless the file is in the current working directory in which case you can just type the filename.

EDIT1: to use textscan instead, the following code should do the trick or at least most of it.

%rt is permission r for read t for open in text mode
csv_file = fopen('D:\Dev\MATLAB\stackoverflow_tests\1.csv','rt');

%the formatspec represents what the scan is 'looking'for. 
formatSpec = '%s%s';

%textscan inputs work in pairs so your scanning the file using the format
%defined above and with a semicolon delimeter
C = textscan(csv_file, formatSpec, 'Delimiter', ';');

fclose(csv_file);

the result is shown.

C{1}{1} =
1,000333e+003
C{1}{2} =
1,001297e+003
C{1}{3} =
1,002261e+003
C{2}{1} =
6,620171e+001
C{2}{2} =
6,519699e+001
C{2}{3} =
6,444984e+001

EDIT2: to replace the comma with a dot and convert to a integer of type double:

[row, col] = size(C);
for kk = 1 : col
    A = C{1,kk};
    converted_data{1,kk} = str2double(strrep(A, ',', '.'));
end

celldisp(converted_data)

result:

converted_data{1} =
   1.0e+03 *
    1.0003
    1.0013
    1.0023
converted_data{2} =
   66.2017
   65.1970
   64.4498
bilaly
  • 536
  • 3
  • 12
  • Unfortunately, by doing this, I am told "Mismatch between file and format string. Trouble reading 'Numeric' field from file (row number 1, field number 2) ==> ,000333e+003;6,620171e+001\n " – Niels Møller Apr 27 '15 at 13:09
  • have you tried using the import data button on the home tab? or does the import have to be in code? you can choose the delimeter type in the gui – bilaly Apr 27 '15 at 13:15
  • 1
    The import data button works (it recognizes the comma and semicolon), but I would really prefer it to be in code/editor, as I've got quite a few files. – Niels Møller Apr 27 '15 at 13:18
  • ok so textscan is what you want.. the semi colon separates the data what is the comma doing? – bilaly Apr 27 '15 at 13:33
  • The comma is used in many non-English countries as we use period "." in English. So: 6,62 = 6.62. – Niels Møller Apr 27 '15 at 14:12
  • 1
    I've credited you, as your suggestion on using the Import button worked in the sense that I made a script from it. Thanks anyway! :) – Niels Møller Apr 29 '15 at 07:39
  • 1
    ok i will add it as an edit in the answer for other users to see it more clearly – bilaly Apr 29 '15 at 16:32
0
% Data is in C:\temp\myfile.csv

fid = fopen('C:\temp\myfile.csv');
data = textscan(fid, '%s%s', 'delimiter', ';');
fclose(fid);

% Convert ',' to '.'
data = cellfun( @(x) str2double(strrep(x, ',', '.')), data, 'uniformoutput', false);


data = 

    [3x1 double]    [3x1 double]

data{1}

ans =

   1.0e+03 *

   1.000333000000000
   1.001297000000000
   1.002261000000000


data{2}

ans =

  66.201710000000006
  65.196990000000000
  64.449839999999995
siliconwafer
  • 732
  • 4
  • 9