Writing X Y values from char to a txt file

Hi,
I have a string with the following structure X1 Y1;X2 Y2; . . . ;Xn Yn (class char).
How can I save this information in a txt file with this structure?
X1 Y1
X2 Y2
. . .
Xn Yn
Thanks

Risposte (2)

Cedric
Cedric il 15 Ott 2013
Modificato: Cedric il 15 Ott 2013
If your char class variable is named str:
fid = fopen( 'myFile.txt', 'w' ) ;
fprintf( fid, strrep( str, ';', '\n' )) ; % Or '\r\n' if you want
fclose( fid ) ; % to be Windows-friendly.

4 Commenti

Thank but it only remove the ; signed and does do the newline
C = '12 14; 13 56; 89 45; 892 45; 89 425; 189 425; 892 15'
fid = fopen( 'd:/myFile.txt', 'w' )
fprintf( fid, strrep( C, ';', '\n' )) ;
fclose( fid ) ;
Cedric
Cedric il 15 Ott 2013
Modificato: Cedric il 15 Ott 2013
It's probably because you opened the file using the notepad under Windows, which needs '\r\n' to interpret newlines correctly. Try with the update that I mention in the comment:
fprintf( fid, strrep( str, ';', '\r\n' )) ;
thx
Image Analyst
Image Analyst il 29 Ott 2013
Modificato: Image Analyst il 29 Ott 2013
Please officially accept the best answer. You can only accept one answer, not more than one.

Accedi per commentare.

n=5
str=regexp(sprintf('X%d Y%d ',repmat(1:n,2)),'X\d\s+Y\d','match')
fid=fopen('file.txt','w')
fprintf(fid,'%s\r\n',str{:})
fclose(fid)

3 Commenti

Thanks. I am new in regexp.
Can you please show me how to implement your code with this char
C = '12 14; 13 56; 89 45; 892 45; 89 425; 189 425; 892 15'
C = '12 14; 13 56; 89 45; 892 45; 89 425; 189 425; 892 15'
str=regexp(C,';','split')
fid=fopen('file.txt','w')
fprintf(fid,'%s\r\n',str{:})
fclose(fid)
thx

Accedi per commentare.

Categorie

Tag

Richiesto:

il 15 Ott 2013

Modificato:

il 29 Ott 2013

Community Treasure Hunt

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

Start Hunting!

Translated by