views:

343

answers:

5

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').

+5  A: 

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
Amro
Thanks Amro! That's exactly what I was looking for.
Ethan White
+2  A: 

If the directory structures are within your home directory you could try building a single path that can be used on both platforms as follows (my Matlab is a bit rough so some of the syntax may not be 100%):

  1. See here for how to get the home directory for the user
  2. Create the path as follows (filesep is a function that returns the file separator for the platform you are running on)

    filepath = [userdir filesep 'MyPath' filesep 'inputfile.csv']

  3. Read the file

    datafile = csvread(filepath)

Otherwise go with Amros answer. It is simpler.

Mark
Thanks Mark. In this case the paths aren't identical due to how Dropbox names it's directory on Linux vs. Windows, but this is good generic solution to the problem and something I appreciate knowing about.
Ethan White
+3  A: 

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.

Edric
+3  A: 

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.

Adrian
+2  A: 

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).
Emil