我一直在Windows中使用MATLAB进行模拟.但是,现在我正在研究Ubuntu,所以当我运行一个加载包含所需数据的文件的模拟时:
auxiliar_nm1 = load('C:\Users\Miguel\Dropbox\Tesina\Simulaciones\Quadrotor Simulation\M Model 1\auxiliar.txt', 'auxiliar_nm1');
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Unable to read file 'C:\Users\Miguel\Dropbox\Tesina\Simulaciones\Quadrotor Simulation\M Model 1\auxiliar.txt'. No such file or directory.
Run Code Online (Sandbox Code Playgroud)
我知道我可以修改路径,所以这个问题将会解决,但我想知道是否可以在MATLAB中确定操作系统,并根据这个参数做出决定,所以这个脚本可用于两种操作系统(Windows和Ubuntu).
该功能computer为您提供所需的功能:
switch computer
case 'PCWIN' % 32-bit Windows
%...
case 'PCWIN64' % 64-bit Windows
%...
case 'GLNXA64' % Linux
%...
case 'MACI64' % Mac
%...
end
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用特定的功能ispc,isunix并且ismac,虽然注意到isunix在Mac返回true.
小智 5
os = system_dependent('getos')
Run Code Online (Sandbox Code Playgroud)
返回操作系统的完整字符串,您可以在条件语句中解析该字符串以运行所需的加载命令.
strncmp(os,'Linux',5)
Run Code Online (Sandbox Code Playgroud)
如果它是Linux,则返回1(仅比较前n个字符,在本例中为5).
if(strncmp(os,'Linux',5)
load('linux dependant path')
else
load('Windows path')
end
Run Code Online (Sandbox Code Playgroud)