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.
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.
--------------------------------------------------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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With