Borrar filtros
Borrar filtros

How do I make %s to work?

7 visualizaciones (últimos 30 días)
Persson121
Persson121 el 18 de Feb. de 2012
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)...."

Respuestas (1)

Walter Roberson
Walter Roberson el 18 de Feb. de 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 comentarios
Image Analyst
Image Analyst el 18 de Feb. de 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
Jiro Doke el 19 de Feb. de 2012
You can also try
fid = fopen(fullfile('program', 'workspace', 'files', lista{a}), 'r');

Iniciar sesión para comentar.

Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by