How do I make %s to work?

7 次查看(过去 30 天)
Persson121
Persson121 2012-2-18
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)...."

回答(1 个)

Walter Roberson
Walter Roberson 2012-2-18
%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 个评论
Image Analyst
Image Analyst 2012-2-18
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 2012-2-19
You can also try
fid = fopen(fullfile('program', 'workspace', 'files', lista{a}), 'r');

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by