MATLAB - change 'filename.jpg' into 'new_filename.jpg'

Hello,

I am not familiar with working with strings in matlab.
Could you please code that will change a filename while leaving its extension?

Thanks!
LVL 1
slaviknAsked:
Who is Participating?
 
gordonkennedyCommented:
Adding text onto the front is relatively easy, so I'm going to modify your question slightly to make the response a bit more uesful ... change "filename.jpg" to "filename_new.jpg";

   >> myStr = 'filename.jpg';
   >> [substrName,substrExt] = strtok(myStr,'.');
   >> myChangedStr = [substrName,'_new',substrExt];

You can rename the file from matlab using :
   >> movefile(myStr,myChangedStr);

or (assuming you are running under Windows), you can issue commands direct to the operating system using the dos function, as follows :
   >> commandStr = sprintf("rename "%s" "%s",myStr,myChangedStr);
   >> dos(commandStr);

Good luck.
0
 
gordonkennedyCommented:
sorry, the penultimate line of code should read :
   >> commandStr = sprintf('rename ''%s'' ''%s''',myStr,myChangedStr);
(note that all quotes are single qoutes)
0
 
slaviknAuthor Commented:
Thanks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.