How do I make %s to work?

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일

0 개 추천

%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

Persson121
Persson121 2012년 2월 18일
Are you sure that is lista{a}? Not lista(a)?
And when I want to close it i write fclose(fid);
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');

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2012년 2월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by