How to make script to function for sprintf command

4 Ansichten (letzte 30 Tage)
Nopparat
Nopparat am 6 Sep. 2012
I want to write this make this script to function
name = regexp(sprintf('test%.3d.tif*',0:10 ), '*', 'split');
I got the result "test000, test001, test002, ..., test010" so I worked Then I want to make it to function so I did
function filename(basename, digits, start_no)
name = regexp(sprintf('basename %.digits d.tif*', start_no:10-start_no-1), '*', 'split');
end but it didn't work so I use %s
function filename(basename, digits, start_no)
name = regexp(sprintf('%s %. %s d.tif*', start_no:10-start_no-1, basename, digits), '*', 'split');
It still didn't work so how can I fix it. Thank you in advance
  2 Kommentare
Matt Fig
Matt Fig am 6 Sep. 2012
Please learn to format your posts, and be more descriptive. What does "it doesn't work" mean? Did MATLAB crash? Did you get an error message (what did it say?)? Did the computer catch fire? Be specific!
Walter Roberson
Walter Roberson am 6 Sep. 2012
It would be less error-prone to use '\*' in the pattern instead of '*'.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt Fig
Matt Fig am 6 Sep. 2012
There are several problems. For one, you don't specify any return argument for your function. Try this one:
function name = filename(basename, digits, start_no)
S = sprintf('%i',digits);
S = sprintf([basename, '%.',S,'d.tif*'], start_no:10-start_no-1);
name = regexp(S(1:end-1), '*', 'split');
Now at the command line:
filename('test', 7, 0)

Weitere Antworten (0)

Kategorien

Mehr zu Argument Definitions finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by