Clear Filters
Clear Filters

Importing a .csv file

150 views (last 30 days)
Farid BOUSSAOUD
Farid BOUSSAOUD on 2 Apr 2024
Commented: Voss on 3 Apr 2024
Hello everyone
I have a .csv file that contains values (juste to illustrate)
Time(S),Cur(A),Vol(V)
0,0,00,4,206
1,0,00,4,206
2,0,52,4,092
4,0,52,4,088
6,0,52,4,085
8,0,52,4,082
10,0,52,4,082
12,0,52,4,081
14,0,52,4,081
16,0,52,4,078
18,0,52,4,074
20,0,52,4,074
22,0,52,4,074
24,0,52,4,073
26,0,52,4,073
28,0,52,4,073
30,0,52,4,073
Under MATLAB, I have written the following code
data = readmatrix('Discharge_Curve.csv')
The output of this code is 5 different vectors, but that's not what I'm looking for. What I exactly want is to have 3 vectors Time(S), Cur(A), Vol(V). For example, for line 1, two commas should be replaced with dots.
0,0,00,4,206 ----->to 0,0.00,4.206
1,0,00,4,206 ----->to 1,0.00,4.206
the final results
0,0.00,4.206
...
2,0.52,4.092
...
The issue arises from my measuring instrument, which outputs the CSV file incorrectly. It doesn't distinguish between commas and dots, so it uses commas for everything.
For this, I am looking for a solution that addresses my issue.
Thank you very much for your very valuable help

Accepted Answer

Jan Kappen
Jan Kappen on 2 Apr 2024
Edited: Jan Kappen on 2 Apr 2024
The right approach is not to have commas as decimal separators in your file. I know this is common in Europe and especially Germany (where I live too). Best advice is to set the region options in Windows to use the dot "." as decimal separator. That advice is based on long experience...
You can tell readmatrix how the delimiter (what is between your numbers) and how the decimal separator shall look like, see below:
BUT: your file is broken. Using a comma for the decimal separator and delimiter is not possible - MATLAB (and even I) can't distinguish between a separator and a decimal number. Can you? How to know if it's a separator or a decimal number?
There might be special cases, e.g. you know that the first column is always an integer, second column always has 2 decimal places etc. But do we know that?
Tell the person you got that file from to use different characters to split decimal numbers and fields in the csv file.
  1 Comment
Farid BOUSSAOUD
Farid BOUSSAOUD on 3 Apr 2024
Hello Kappen,
I am deeply grateful for your response. You provided an effective solution to the core of the issue. I adjusted the Windows settings, and now I'm receiving the data correctly. Thank you once again.

Sign in to comment.

More Answers (1)

Voss
Voss on 2 Apr 2024
Here's one way you can try to fix that file:
f_in = 'data.csv';
f_out = 'data_modified.csv';
% show the original file's contents
type(f_in)
Time(S),Cur(A),Vol(V) 0,0,0,4,206 1,0,0,4,206 2,0,52,4,92 4,0,52,4,88 6,0,52,4,85 8,0,52,4,82 10,0,52,4,82 12,0,52,4,81 14,0,52,4,81 16,0,52,4,78 18,0,52,4,74 20,0,52,4,74 22,0,52,4,74 24,0,52,4,73 26,0,52,4,73 28,0,52,4,73 30,0,52,4,73
% read the file
str = fileread(f_in);
% replace the 2nd and 4th commas on each line with periods
str = regexprep(str,'(\r?\n\d+),(\d+),(\d+),(\d+),(\d+)','$1,$2.$3,$4.$5');
% write the new file
fid = fopen(f_out,'w');
fprintf(fid,'%s',str);
fclose(fid);
% show the new file's contents
type(f_out)
Time(S),Cur(A),Vol(V) 0,0.0,4.206 1,0.0,4.206 2,0.52,4.92 4,0.52,4.88 6,0.52,4.85 8,0.52,4.82 10,0.52,4.82 12,0.52,4.81 14,0.52,4.81 16,0.52,4.78 18,0.52,4.74 20,0.52,4.74 22,0.52,4.74 24,0.52,4.73 26,0.52,4.73 28,0.52,4.73 30,0.52,4.73
% now readmatrix returns a matrix with three columns
M = readmatrix(f_out)
M = 17x3
0 0 4.2060 1.0000 0 4.2060 2.0000 0.5200 4.9200 4.0000 0.5200 4.8800 6.0000 0.5200 4.8500 8.0000 0.5200 4.8200 10.0000 0.5200 4.8200 12.0000 0.5200 4.8100 14.0000 0.5200 4.8100 16.0000 0.5200 4.7800
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  2 Comments
Farid BOUSSAOUD
Farid BOUSSAOUD on 3 Apr 2024
Hello. Thank you very much for your efforts. I found a solution that solves this problem definitively without resorting to MATLAB code. Thanks once again.
Voss
Voss on 3 Apr 2024
You're welcome!
Changing settings may be the best way to prevent the problem for files acquired in the future, but I'll leave my answer here since it may be useful for fixing files already acquired.

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!