MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何在路径位置中包含通用文件名(在Matlab中作为函数参数)? (https://www.labfans.com/bbs/showthread.php?t=23971)

poster 2019-12-10 20:48

如何在路径位置中包含通用文件名(在Matlab中作为函数参数)?
 
[B]我想要的摘要:[/B]如果我将文件名作为函数的参数,则如何在路径位置包括该文件名,以便该路径名中的文件名是一个用户输入的。如果您不明白我在说什么,请阅读以下说明:

[B]二次说明:[/B]

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

同样,我想为Reportq_rwd和Reportq_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')

[B]回答:[/B]

如果将以下内容连接起来:

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



[url=https://stackoverflow.com/questions/3868842]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 23:18

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.