Importing a .csv file
420 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Jan Kappen
2024-4-2
编辑:Jan Kappen
2024-4-2
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 个)
Voss
2024-4-2
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)
% 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)
% now readmatrix returns a matrix with three columns
M = readmatrix(f_out)
2 个评论
Voss
2024-4-3
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!