views:

1185

answers:

2

I'm trying to programmatically rename a file in the working directory from a = 'temp.txt' to b = 'hello.txt'. How would you suggest doing so? Is there an easy file renaming function in MATLAB?

+4  A: 

I think you're looking for MOVEFILE.

mtrw
Yes, that is what I ended up doing.
stanigator
+2  A: 

Here's a list of a few solutions:

  • Use the MOVEFILE function (as suggested by mtrw).
  • Use the SYSTEM function to execute an operating system command. For example (on Windows):

    system('rename temp.txt hello.txt');
    system(['rename ' a ' ' b]);  % If the file names are stored in strings
    
  • Use the shell escape operator (!) to invoke a system command. For example (on Windows):

    !rename temp.txt hello.txt
    

    If the file names are stored in strings, you would need to use EVAL:

    a = 'temp.txt';
    b = 'hello.txt';
    eval(['!rename ' a ' ' b]);
    
gnovice

related questions