[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1723
  • Last Modified:

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!
0
slavikn
Asked:
slavikn
  • 2
1 Solution
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now