Is it possible that the MATLAB script behaves differently depending on the OS on which it is running?

I am running MATLAB on Linux and Windows XP. My files are synchronized between all the computers that I use, but due to differences in the directory structure between Linux and Windows, I have to have separate import and export lines for different operating systems. Right now I'm just commenting out a line for the wrong OS, but I'm 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 when you need to execute system commands from MATLAB with system('command').

+5
source share
5 answers

ispc/ isunix/ ismac computer

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

Amro, , .

.

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

, , , , . , .

+4

, fileparts fullfile , UNIX, Windows, , .

+3

, . , . .

:

  • fileRoot . fullfile .
  • , . / , .
  • (, ).
+3

, , ( Matlab , 100%):

  • ,
  • (filesep - , , )

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

  • datafile = csvread ( )

Amros. .

+2

All Articles