Effacer les filtres
Effacer les filtres

How can I write both number and text to a file?

9 vues (au cours des 30 derniers jours)
Andrea Kursetgjerde
Andrea Kursetgjerde le 19 Oct 2016
Commenté : Chaya N le 19 Oct 2016
I want to write something like this:
BEGIN 0.00 0,0 0.5,1 1,1.5 1.5,1.75 14,2
from matlab to a text file. There are supposed to be a new number on one line, going down. I also have to go this several times for different values. How can I do this effectively?

Réponse acceptée

Chaya N
Chaya N le 19 Oct 2016
Modifié(e) : Chaya N le 19 Oct 2016
The easiest way to do this would be to write the entire line as a string into a text file, something like:
x = 'BEGIN 0.00 0,0 0.5,1 1,1.5 1.5,1.75 14,2';
fid = fopen('SomeTextFile.txt','a');
fprintf(fid,'%s\n',x);
fclose(fid);
You would have to reassign x every time with your new data. Does this help?
  3 commentaires
Massimo Zanetti
Massimo Zanetti le 19 Oct 2016
Modifié(e) : Massimo Zanetti le 19 Oct 2016
This thing doesn't print anything. Try adding file identifier in the fprintf command..
fprintf(fid,'%s\n',x);
Chaya N
Chaya N le 19 Oct 2016
My bad! I corrected it.

Connectez-vous pour commenter.

Plus de réponses (1)

Massimo Zanetti
Massimo Zanetti le 19 Oct 2016
Modifié(e) : Massimo Zanetti le 19 Oct 2016
Look here:
Here it is:
x = 'BEGIN 0.00 0,0 0.5,1 1,1.5 1.5,1.75 14,2';
fid = fopen('SomeTextFile.txt','a');
fprintf(fid,'%s\n',x);
fclose(fid);
But, for more complex inputs I suggest to read the help page. Will be useful.

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by