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

Posted on 2006-04-18
Last Modified: 2016-02-10

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

Question by:slavikn
    LVL 2

    Accepted Solution

    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.
    LVL 2

    Expert Comment

    sorry, the penultimate line of code should read :
       >> commandStr = sprintf('rename ''%s'' ''%s''',myStr,myChangedStr);
    (note that all quotes are single qoutes)
    LVL 1

    Author Comment

    Thanks :)

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Introduction On a scale of 1 to 10, how would you rate our Product? Many of us have answered that question time and time again. But only a few of us have had the pleasure of receiving a stack of the filled out surveys and being asked to do somethi…
    Lithium-ion batteries area cornerstone of today's portable electronic devices, and even though they are relied upon heavily, their chemistry and origin are not of common knowledge. This article is about a device on which every smartphone, laptop, an…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now