How do I make %s to work?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi! I want to open a file with this code.
lista = {'hello', 'godday'};
a = 1;
fid = fopen('program\workspace\files\%s', lista(a) 'r');
But it dosen't work! Why does this part not work? "....files\%s', lista(a)...."
0 commentaires
Réponses (1)
Walter Roberson
le 18 Fév 2012
%s and the like are recognized in fprintf() and sprintf() but not in other functions.
fid = fopen( sprintf('program\\workspace\\files\\%s', lista{a}), 'r');
Notice also the other changes that had to be made for use with sprintf()
3 commentaires
Image Analyst
le 18 Fév 2012
list is a cell array. Each element of list is a cell. So list(2) is the second cell and is, itself, a cell. The braces mean "contents of" so list{2} means the "contents of" the second cell. In that cell could be anything: an integer, a double, a structure, even another cell. In your case it's a string (character array). So in your case, list(2) is a cell, BUT list{2} is a string, which can then be printed out with the %s format specifier. Make sense?
By the way, you may find it easier to use forward slashes. Believe it or not, Windows can also use forward slashes - you don't need backslashes.
And are you sure your files don't have any extension? It's possible that you told Windows to hide extensions for known file types so that they really have an extension even though you don't see it.
Jiro Doke
le 19 Fév 2012
You can also try
fid = fopen(fullfile('program', 'workspace', 'files', lista{a}), 'r');
Voir également
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!