Skip to main content

13.2) Using System Commands


  • From the Matlab command prompt it is possible to run system commands
  • Under Windows you can use the Matlab command system and for Unix/Linux you can use unix
  • If you are writing a script that you want to be machine independent, then you would first have Matlab check what your system is by using the logical statements isunix or ispc
  • For example:

[matlab]
>> old_filename='raw.dat';
>> new_filename='copy.dat'
>> if (isunix==1)
>> [status result]=unix(['cp ', old_filename, ' ', new_filename])
>> % status equals 0 if successful
>> % results is a string containing the output of tghe Unix command
>> elseif (ispc==1)
>> [status1 result1]=system(['copy ', old_filename, ' ', new_filename])
>> end
[/matlab]

  • Here you can see that we have logical statements that check which operating system is being used and this dictates what copy command is run
  • System commands return a "status" and a "result" string. These can be examined to see if your command ran
  • System commands are very powerful, so be careful when Matlab generates and runs commands. You don't want to delete or overwrite files that you may require later