Reading semicolon delimited CSV as decimal mark in matlab

My problem is that I have CSV data in 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 data in matlab but csvread

requires it to be comma separated and I couldn't find a comma-decimal mark solution. I guess I can use it somehow textscan

?

Sorry to ask for such a (I think) simple question, but I hope someone can help. None of the other questions / answers here seem to be relevant to this combination of comma and semicolon.

+3


source to share


2 answers


EDIT3 (ACCEPTED ANSWER): Using the import data button in the variables section of the home toolbar, you can customize how the data is imported. once that's done, you can click the import selection under the arrow and generate a script or function that follows the same rules as specified in the import data window.

import Data Instructions

----------------------------------------------- --- retained for reference ------------------------------------- ---------- ---

You can use dlmread , it works in the following format

M = dlmread(filename,';')

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 simply enter the filename.

EDIT1: Instead of textscan , 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 an 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

      

+7


source


% 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

      



0


source







All Articles