How to pass filepath variables to mex command?

十年热恋 提交于 2019-12-04 17:58:20

This should do the trick:

input = 'src/helloworld.cpp';
output = '/mexFiles';

eval(['mex ' input ' -outdir ' output]);

When executing a command of the form

func arg1 arg2 arg3

MATLAB treats this the same as

func('arg1', 'arg2', 'arg3')

Contrariwise, when you do something like

input = ' -outdir C:/Users/ian/mexTesting/mexFiles'
mex('src/helloworld.cpp', input)

It's the same as if you had run

mex src/helloworld.cpp ' -outdir C:/Users/ian/mexTesting/mexFiles'

Which is not the same as

mex src/helloworld.cpp -outdir C:/Users/ian/mexTesting/mexFiles

If you wanted to parameterize the arguments to MEX, you need to keep each space-delimited argument in a separate string. So, for example, you could use

outdirArg = 'C:/Users/ian/mexTesting/mexFiles';
mex('src/helloworld.cpp', '-outdir', outdirArg);

If you want to batch a bunch of arguments in a single variable, you could use a cell array and comma-separated list expansion, which would look like this:

args = {'-outdir', 'C:/Users/ian/mexTesting/mexFiles'};
mex('src/helloworld.cpp', args{:});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!