Convert comma to dot

I have a text file similar to this column:

0,472412 0,455627 0,439148 0,421753 ... 0,116577 0,086670 0,057373 0,027161 

How to convert comma to dot in matlab?

+4
source share
4 answers

This post on Matlabs website offers:

 function comma2point_overwrite( filespec ) % replaces all occurences of comma (",") with point (".") in a text-file. % Note that the file is overwritten, which is the price for high speed. file = memmapfile( filespec, 'writable', true ); comma = uint8(','); point = uint8('.'); file.Data( transpose( file.Data==comma) ) = point; delete(file) end 
+4
source

... or you can do this:

 wholefile = fileread('yourfile.txt') % read in entire file newfiledata = strrep(wholefile,',','.') % replace commas with full stops fileid = fopen('yourfile.txt','w') % open file to write fprintf(fileid,'%s',newfiledata) % print to file fclose(fid2) 
+2
source

Another option (since you want to open the file in the matlab workspace):

  • upload file using

      a=load('filename.txt'); 

Since the comma is the default separator, you get a 2xN matrix, for example:

 a = 0 472412 0 455627 0 439148 0 421753 
  • Find a significant number of digits to get the correct decimal point position:

      d = 10.^(1+floor(log10(abs(a(:,2))))); 
  • then

      v=a(:,2)./d 

will give you the vector you need. Now you can save it back to a file or do something ...

+1
source

a = '0.00445'

a = 0.00445

B = strrep (a, ``, '')

b = 0.00445

+1
source

Source: https://habr.com/ru/post/1443021/


All Articles