Replace comma with dot after fopen
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Daniele Sonaglioni
am 19 Okt. 2021
Kommentiert: Star Strider
am 20 Okt. 2021
Hello everyone,
I am having some problems with Matlab.
I import a txt file with fopen and, after imported, I want to convert the commas in the file with dots but I do not know how to do. I have searched between the answers in the Matlab Central but I have found nothing.
Thank you for the help.
4 Kommentare
Akzeptierte Antwort
Star Strider
am 19 Okt. 2021
For MATLAB versions R2019a and later, the readmatrix function permits definition of the 'DecimalSeparator' (see the Text Files Only documentation section, since ther is no direct link to it) as a name-value pair argument.
.
4 Kommentare
Star Strider
am 20 Okt. 2021
Since you stated that using readmatrix (or readtable) would disrupt your existing code, an alternative would be textscan.
The online Run feature would not let me do all this here, so I did it offline —
fidi = fopen('trial_file.txt','rt');
C = textscan(fidi, '%f%s%s%s', 'HeaderLines',10, 'CollectOutput',1, 'EndOfLine','\r\n');
fclose(fidi);
M = [C{1} str2double(strrep(C{2},',','.'))];
Check = M(1:5,:)
Check =
0 0 -19.773 -0.009783
1 0.001 -19.733 -0.015429
2 0.002 -19.655 -0.038148
3 0.003 -19.564 -0.081012
4 0.004 -19.462 -0.13152
That imports the file and coinverts it correctly so it can be used in computations.
.
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!