如何在路径位置中包含一般文件名(在 Matlab 中作为函数参数)?

发布于 2024-09-26 05:53:29 字数 1761 浏览 3 评论 0原文

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

二级说明:

我正在制作一个通用函数,需要调用一个名为 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')

BRIEF SUMMARY OF WHAT I WANT: If I take a file name as argument of a function, how do I include that file name in the path location such that the name of the file in the path location is the one user enters. If you didn't understand what I am saying, then read below for explanation:

SECONDARY EXPLANATION:

I am making a general function which requires calling a software called CMG. The software needs a .dat file, whose name I am taking as argument in the function with general name in the argument as ReservoirModel_CMGBuilder. As you can see in the partial code below, this ReservoirModel_CMGBuilder file is kept in a folder whose path location I have entered. However the problem is since the file name is in quotes, so it does not identify the file name in the code. What I want is I take name of the .dat file name, needed for CMG, from the user and store it in name ReservoirModel_CMGBuilder, and then use this name in the path location to pick up that file.

Similarly I want to do this thing for Reportq_rwd and Reportq_rwo. How can I do this?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉徒眸意 2024-10-03 05:53:29

如果你连接这样的东西:

['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 中使用 \,则需要使用反斜杠对其进行转义。

If you concatenate something like this:

['foo"' bar '"baz']

you get a string like this: foo"bar"baz

So for your question:

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 '"'])

It might be easier to use sprintf:

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

Also not the use of fullfile to construct the path name -- it'll automatically insert the right kind of path separator. Note if you want to use a \ in sprintf you need to escape it with a backslash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文