Effacer les filtres
Effacer les filtres

fprintf: writing text lists to .txt files

8 vues (au cours des 30 derniers jours)
William
William le 28 Oct 2011
I need some help exporting some text lists in cell arrays to .txt functions. I have a list of names in an n x 1 array, called 'assets'. I would like to export it to a .txt file ('assets.txt'). My code currently states:
fid = fopen('assets.txt', 'w');
for row = 1:length(assets)
fprintf(fid, '%s\n', assets(row));
end
fclose(fid)
I get this error message: "Function is not defined for 'cell' inputs." What am I doing wrong?

Réponse acceptée

Grzegorz Knor
Grzegorz Knor le 28 Oct 2011
You have to change round brackets into curly brackets:
assets = {'first','second','third','fourth'};
fid = fopen('assets.txt', 'w');
for row = 1:length(assets)
fprintf(fid, '%s\n', assets{row});
end
fclose(fid);
  1 commentaire
William
William le 7 Déc 2011
It took a while for me to get back to this, but: thanks, that did the trick.

Connectez-vous pour commenter.

Plus de réponses (2)

Grzegorz Knor
Grzegorz Knor le 28 Oct 2011
Probably:
fprintf(fid, '%s\n', assets{row});

William
William le 28 Oct 2011
...Ok, let me rephrase. How do I need to phrase my code to write my list to a .txt file?

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by