Effacer les filtres
Effacer les filtres

How to make script to function for sprintf command

1 vue (au cours des 30 derniers jours)
Nopparat
Nopparat le 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 commentaires
Matt Fig
Matt Fig le 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 le 6 Sep 2012
It would be less error-prone to use '\*' in the pattern instead of '*'.

Connectez-vous pour commenter.

Réponse acceptée

Matt Fig
Matt Fig le 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)
  1 commentaire
Nopparat
Nopparat le 9 Sep 2012
It works very well! Thank you very much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Argument Definitions dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by