Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
回复
 
主题工具 显示模式
旧 2019-12-10, 20:48   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何在路径位置中包含通用文件名(在Matlab中作为函数参数)?

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

二次说明:

我正在执行一项常规功能,需要调用一个名为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')

回答:

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

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



更多&回答...
poster 当前离线   回复时引用此帖
回复

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



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


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