How to apply a generated filename to the mex command?

16 Ansichten (letzte 30 Tage)
Christoph
Christoph am 28 Okt. 2025 um 6:15
Kommentiert: Christoph am 28 Okt. 2025 um 7:07
In a .m file, I am able to run mex on a file e.g. mex myfile8_1.c and then successfully call the resulting mex function with myfile8_1()
In fact the "8_1" component may change, so I construct a string and use str2func() to generate the function name to use. However when I construct a string to use as input for mex, mex fails e.g.
script_version = "8_1";
process_filename = strcat("myfile", script_version, ".c");
mex process_filename
This results in an error that no file named process_filename exists in the current directory (but of course "myfile8_1.c" does exist).
Where str2func() is able to convert a string to a function variable, is there something similar which will convert a string into a filename variable in a way acceptable to the mex command?
Alternatively is there a way to tell mex to treat the next input is a variable rather than an actual filename?
Thanks for any help,
chris

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 28 Okt. 2025 um 6:32
script_version = "8_1";
process_filename = strcat("myfile", script_version, ".c");
mex(process_filename)
  1 Kommentar
Christoph
Christoph am 28 Okt. 2025 um 7:07
If I have additional .c files to be compiled at the same time, can I then just add more filename variables?
e.g.
mex(process_filename, ran1_filename, gasdev_filename)
(where ran1_filename = "ran1.c" and gasdev_filename = "gasdev.c") ??
It seems to work.
Thanks,
chris

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Help Center und File Exchange

Produkte


Version

R2025b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by