Replace comma with dot after fopen

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 Commenti

KSSV
KSSV il 19 Ott 2021
Read about strrep.
"I have searched between the answers in the Matlab Central but I have found nothing"
Here are 339 results, many of which are related to file importing:
@KSSV Thanks but it does not help me because it must be used before fopen (as far as I have seen).
I attach a part of my code to let you know what I am working on:
fidi = fopen('myfile.txt','rt');
k1 = 1;
while ~feof(fidi)
readline = fgetl(fidi);
if strcmp(readline,'Results:')
C = textscan(fidi, '%f%f%f%f %*f%*f%*f%*f%*f%*f', 'HeaderLines',9, 'CollectOutput',1);
else
C = textscan(fidi, '%f%f%f%f %*f%*f%*f%*f%*f%*f', 'HeaderLines',0, 'CollectOutput',1);
end
M = cell2mat(C);
fprintf(1,'\tSection %2d: (%4d x %d)\n', k1, size(M));
if isempty(M) % Empty Matrix Indicates End-Of-File
fprintf(1,'Reading finished, %d Sections\n',k1-1);
break
end
D{k1,:} = M;
fseek(fidi, 0, 0);
k1 = k1 + 1;
end
fclose(fidi);
@Stephen I am aware that there are results on the Matlab Central (I have also checked them) but I have not found anything useful.
I know that before asking question one has to check on the central.

Accedi per commentare.

 Risposta accettata

Star Strider
Star Strider il 19 Ott 2021

0 voti

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 Commenti

Your idea is quite good but using readmatrix will result in a great change in the code and this is something I want to avoid. I have attached the code in my previous answer, maybe you remeber it: can you suggest something about that code?
I don’t remember it, however that looks like my code.
One possibility would be to read the variables in a strings rather than numeric, then use strrep on the strings, and then read the entire line using textscan.
Another option would be to read the entire file as text using the fileread funciton, then do the conversion and use textscan, since it can take character vectors as input.
Attaching the file would help.
.
This the kind of file I want to convert.
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.
.

Accedi per commentare.

Più risposte (1)

Stephen23
Stephen23 il 20 Ott 2021
Modificato: Stephen23 il 20 Ott 2021
fnm = 'trial_file.txt';
opt = detectImportOptions(fnm, 'VariableNamesLine',9, 'VariableUnitsLine',10, 'DecimalSeparator',',', 'ExtraColumnsRule','ignore');
opt.DataLines = [11,Inf];
tbl = readtable(fnm, opt)
tbl = 69227×4 table
Index t Ts Value _____ _____ _______ _________ 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 5 0.005 -19.36 -0.17076 6 0.006 -19.262 -0.19742 7 0.007 -19.161 -0.21605 8 0.008 -19.061 -0.22971 9 0.009 -18.96 -0.24071 10 0.01 -18.861 -0.24984 11 0.011 -18.76 -0.25674 12 0.012 -18.661 -0.26249 13 0.013 -18.56 -0.26758 14 0.014 -18.461 -0.27248 15 0.015 -18.36 -0.27657

Categorie

Scopri di più su Data Import and Analysis in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by