Hello i have a file path which is this '\\psf\Home\Documents\MATLAB\silhouettes\1\00_1' This points to a set of images that i need to add to matlab which works fine. what i need to happen is that when this code is run it changes the one to a two so it would look like this '\\psf\Home\Documents\MATLAB\silhouettes\2\00_1' and then change to three and four etc, i have been told i can use sprintf to do this but cannot see a way to make it work, i don't suppose any out there has any sprintf knowledge who could help me out?
thanks Rob

 채택된 답변

Matt Fig
Matt Fig 2011년 2월 26일

2 개 추천

I assume you are using the string in a loop. If not, adjust accordingly.
for ii = 1:5
S = sprintf('\\\\psf\\Home\\Documents\\MATLAB\\silhouettes\\%i\\00_1\n',ii)
end
.
.
.
.
In reply to your email:
The SPRINTF function creates a formatted string. the %i is the format specifier which tells SPRINTF to turn the argument (ii) into an integer. Also notice that you had to have two backslashes everywhere you wanted a single backslash printed. The backslash is the escape character, so SPRINTF doesn't print what is after it (like the \n at the end). I recommend reading the doc on this function, because it comes up all the time!

댓글 수: 4

rob wise
rob wise 2011년 2월 26일
ah i see, thanks for that. is there a simple way to make it run the function after it increases the number each time, because at the moment its only adding the last photo set. i hope i'm making sense and not sounding to stupid, i've never really played with this before.
Matt Fig
Matt Fig 2011년 2월 26일
What function? Are using the string to change paths, then you want the function to run? If so, you should simply call the function after the path is changed, assuming your function is located somewhere on the MATLAB search path (i.e., it can be reached from any folder).
rob wise
rob wise 2011년 2월 26일
basically the path points to a folder with different images, when the program is run it adds to images to a database, i need it to change the path name, then run the program so it imports the images, then change the path name and adds the images and carries that on.
Matt Fig
Matt Fig 2011년 2월 27일
You will want to use the CD function on your paths generated above, and also to get back. Just make sure to save the main path first, something like:
P = pwd;
for ii = 1:?
% Create S as above.
cd(S)
% call function, create variables, whatever
cd(P)
% Store variables in database, whatever
end

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 2월 26일

1 개 추천

Adjusting Matt's answer slightly to make it more intuitive to program:
for ii = 1:5
S = sprintf('%s%d%s, '\\psf\Home\Documents\MATLAB\silhouettes\', ii, '\00_1')
end

댓글 수: 4

rob wise
rob wise 2011년 2월 26일
it didnt like it, threw this up
??? S = sprintf('%s%d%s,'\\psf\Home\Documents\MATLAB\silhouettes\', ii, '\00_1')
|
Error: Unexpected MATLAB operator.
Matt Fig
Matt Fig 2011년 2월 26일
Walter meant:
S = sprintf('%s%d%s', '\\psf\Home\Documents\MATLAB\silhouettes\', ii, '\00_1')
This is the multi-arg version of the solution!
Matt Fig
Matt Fig 2011년 2월 26일
Speaking of which... It is funny Walter, I learned from you (NG posts) to inline my strings to SPRINTF, rather than passing multiple arguments and one formatter string at the beginning (how I always used to do it)!
Walter Roberson
Walter Roberson 2011년 2월 27일
Yeah, but I don't program on Windows with all those \ characters in pathnames. When I start getting strings with numerous \ then I switch styles.
Well, actually if I were programming it myself, I would code inline but use / instead of \\ since Windows will accept / in place of \ .

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

카테고리

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

태그

질문:

2011년 2월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by