How to pass filepath variables to mex command?

2019-04-14 07:54发布

Currently trying to create a minimal example of scripted mex file generation.

I have a MATLAB .m script that I am running to generate mex files. I would like to pass in all the arguments as variables so that I can automate a bunch of mex file construction when given a list of filenames/paths.

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

#2 : unknown filepath / can't find location (interprets entire string as path to mex file)
input = 'src/helloworld.cpp -outdir C:/Users/ian/mexTesting/mexFiles'
mex(input)

#3 : same issue as #2
mex input

#4 : Works, but no variables included, so no easy way to automate
mex src/helloworld.cpp -outdir /mexFiles

All of these (except #4) get either unknown argument -outdir or it interprets the input char arr as a path to a mex file.

Does anyone know how to pass variables into the mex command?

There are no questions that have actually answered this issue that I could find. Any help would be appreciated.

标签: c++ matlab mex
2条回答
叛逆
2楼-- · 2019-04-14 08:18

This should do the trick:

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

eval(['mex ' input ' -outdir ' output]);
查看更多
再贱就再见
3楼-- · 2019-04-14 08:21

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{:});
查看更多
登录 后发表回答