Filter löschen
Filter löschen

How do I make %s to work?

7 Ansichten (letzte 30 Tage)
Persson121
Persson121 am 18 Feb. 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)...."

Antworten (1)

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by