【问题标题】:How to include the general file name, taken as function argument in Matlab, in the path location?如何在路径位置包含通用文件名,作为 Matlab 中的函数参数?
【发布时间】:2010-10-06 00:58:48
【问题描述】:

我想要的内容的简要总结:如果我将文件名作为函数的参数,我如何在路径位置中包含该文件名,以便路径中的文件名location 是用户输入的位置。如果您不明白我在说什么,请阅读下面的解释:

次要解释:

我正在制作一个需要调用 CMG 软件的通用功能。该软件需要一个.dat 文件,我将其名称作为函数中的参数,参数中的通用名称为ReservoirModel_CMGBuilder。正如您在下面的部分代码中看到的那样,这个ReservoirModel_CMGBuilder 文件保存在我输入的路径位置的文件夹中。但是问题是因为文件名是用引号引起来的,所以它不能识别代码中的文件名。我想要的是从用户那里获取 CMG 所需的 .dat 文件名的名称并将其存储在名称 ReservoirModel_CMGBuilder 中,然后在路径位置中使用该名称来获取该文件。

同样,我想为Reportq_rwdReportq_rwo 做这件事。我该怎么做?

function [q_yearly,Swav_yearly]=q_from_CMG_general(nModel,nCell,T,ReservoirModel_CMGBuilder,poro_models_gslib_file,perm_models_gslib_file,Reportq)

ReservoirModel_CMGBuilder=[ReservoirModel_CMGBuilder '.dat']; % string concatenation
Reportq_rwd=[Reportq '.rwd'];
Reportq_rwo=[Reportq '.rwo'];

poro_models=gslib_file_to_matlab_var(nModel,nCell,poro_models_gslib_file);
perm_models=gslib_file_to_matlab_var(nModel,nCell,perm_models_gslib_file);

%% loop to run all nModel

for model_no=1:nModel

    %% Writing the porosity and permeability model one at a time in .inc file, which will be read and will work as input to porosity and permeability models in CMG

    dlmwrite('poro_model.inc',poro_models(:,model_no),'delimiter','\n');
    dlmwrite('perm_model.inc',perm_models(:,model_no),'delimiter','\n');

    %% Prior to driling an exploratory well or acquiring a seismic with just 5 producing wells

    %# Calls CMG
    system('mx200810.exe -f "C:\Documents and Settings\HSingh2\Desktop\Work\Model - SGEMS, CMG and MATLAB\ReservoirModel_CMGBuilder"') % Calls CMG

    %# Calls parameter report file and generates output file
    system('report.exe /f Reportq_rwd /o Reportq_rwo')

【问题讨论】:

    标签: matlab function path location


    【解决方案1】:

    如果你连接这样的东西:

    ['foo"' bar '"baz']
    

    你会得到这样的字符串:foo"bar"baz

    所以对于你的问题:

    system(['mx200810.exe -f "C:\Documents and Settings\HSingh2\Desktop\Work\Model - SGEMS, CMG and MATLAB\' ReservoirModel_CMGBuilder '"']) % Calls CMG
    
    %# Calls parameter report file and generates output file
    system(['report.exe /f "' Reportq_rwd '" /o "' Reportq_rwo '"'])
    

    使用sprintf可能更容易:

    system(sprintf('mx200810.exe -f "%s"', ...
        fullfile('c:', 'Documents and Settings', 'HSingh2', ...
                 'Desktop', 'Work', 'Model - SGEMS, CMG and MATLAB', ...
                 ReservoirModel_CMGBuilder)));
    

    也不使用fullfile 来构造路径名——它会自动插入正确的路径分隔符。请注意,如果您想在 sprintf 中使用 \,则需要使用反斜杠对其进行转义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多