Clear Filters
Clear Filters

How to make script to function for sprintf command

1 view (last 30 days)
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 Comments
Matt Fig
Matt Fig on 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 on 6 Sep 2012
It would be less error-prone to use '\*' in the pattern instead of '*'.

Sign in to comment.

Accepted Answer

Matt Fig
Matt Fig on 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)

More Answers (0)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!