Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for a MATLAB script to behave differently depending on the OS it is being executed on?

I run MATLAB on both Linux and Windows XP. My files are synced among all of the computers I use, but because of the differences in directory structure between Linux and Windows I have to have separate import and export lines for the different operating systems. At the moment I just comment out the line for the wrong OS, but I am wondering if it is possible to write something like:

if OS == Windows
    datafile = csvread('C:\Documents and Settings\Me\MyPath\inputfile.csv');
else
    datafile = csvread('/home/Me/MyPath/inputfile.csv');
end

This is also a more general question that applies in cases where one wants to execute system commands from within MATLAB using system('command').

like image 691
Ethan White Avatar asked Oct 15 '09 21:10

Ethan White


4 Answers

You can use ispc/isunix/ismac functions to determine the platform or even use the computer function for more information about the machine

if ispc
    datafile = csvread('C:\Documents and Settings\Me\MyPath\inputfile.csv');
else
    datafile = csvread('/home/Me/MyPath/inputfile.csv');
end
like image 174
Amro Avatar answered Nov 17 '22 05:11

Amro


To follow up on Amro's answer, I was going to just make a comment but struggled with the formatting for the code.

I'd prefer to split the OS selection from the file read.

if ispc 
    strFile = 'C:\Documents and Settings\Me\MyPath\inputfile.csv'; 
else 
    strFile = '/home/Me/MyPath/inputfile.csv'; 
end 

try
    datafile = csvread(strFile);
catch
    % setup any error handling
    error(['Error reading file : ',strFile]);
end

That way if I need to change the way the file is read, perhaps with another function, it's only one line to change. Also it keeps the error handling simple and local, one error statement can handle either format.

like image 25
Adrian Avatar answered Nov 17 '22 05:11

Adrian


Just to add a minor point to the existing good answers, I tend to use fileparts and fullfile when building paths that need to work on both UNIX and Windows variants, as those know how to deal with slashes correctly.

like image 3
Edric Avatar answered Nov 17 '22 04:11

Edric


In addition to using the various techniques here for dealing with path and file separator differences, you should consider simply trying to avoid coding in absolute paths into your scripts. If you must use them, try to put them in as few files as possible. This will make your porting effort simplest.

Some ideas:

  • Set a fileRoot variable at some early entry point or config file. Use fullfile or any of the other techniques for constructing a full path.
  • Always use relative paths, relative to where the user is operating. This can make it easy for the user to put the input/output wherever is desired.
  • Parameterize the input and output paths at your function entries (e.g., via a system specific context object).
like image 3
Emil Sit Avatar answered Nov 17 '22 03:11

Emil Sit