All,
Is there a way, within a mexFunction, of retrieving the actual name of (the caller's) argument passed as one of the prhs parameters? Or, in other words, is there a way in MEX to do the equivalent of the inputname function as in
function f(a)
fprintf('Formal parameter ''a'' is called ''%s''\n', ...
inputname(1))
end

 採用された回答

Kaustubha Govind
Kaustubha Govind 2011 年 11 月 10 日

0 投票

I don't think there is a MEX-API function that is the equivalent, but you can always use mexCallMATLAB to dispatch the call to MATLAB itself.

7 件のコメント

Bård Skaflestad
Bård Skaflestad 2011 年 11 月 10 日
Right,
I haven't actually tested it, so I'm speculating here, but won't dispatching to, say, |inputname(n)| report the |n|-th parameter in the *caller's* parameter list (if there is such a thing) and not the |n|-th argument of the |mexFunction| parameter list?
Bård Skaflestad
Bård Skaflestad 2011 年 11 月 10 日
No, it doesn't (return caller's values). This method is exactly what I'm looking for.
To give a bit of perspective, I'm trying to call an anonymous function from a MEX function. As far as I understand, function handles aren't exposed in the MEX-API and there is no equivalent of mexCallMATLAB that accepts (something akin to) a function pointer.
In my case, the function handle is stored in a structure field and the best way I could think of to achieve my goal was to dynamically form a string like
"a = s.f(rand(10))"
with |s| being the variable whose name I'd derive from 'inputname' and then to use mexEvalString (or the WithTrap variant) to affect the function call. Is there some other way?
Jan
Jan 2011 年 11 月 10 日
You can use mexCallMATLAB to call FEVAL with the function handle as argument. Perhaps somebody knows how to use mxFevalFunctionHandle?
Bård Skaflestad
Bård Skaflestad 2011 年 11 月 10 日
Ah, of course. Why didn't I think of that? The convenience of function handles means I haven't used FEVAL in probably about six years. I'd pretty much forgotten its existence...
I'll see how the FEVAL method pans out. Thanks a lot!
Jan
Jan 2011 年 11 月 10 日
Sorry, my comment should be an answer actually.
Kaustubha Govind
Kaustubha Govind 2011 年 11 月 11 日
This solution has an attached example where a function handle is passed into a MEX-function, which is then evaluated using mexCallMATLAB and feval: http://www.mathworks.com/support/solutions/en/data/1-9RKXIN/index.html - I believe this is what Jan is suggesting.
Bård Skaflestad
Bård Skaflestad 2011 年 11 月 11 日
I suppose so, too. I've tried it now and it works perfectly. Thank you so much, both of you. This was really helpful.

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2011 年 11 月 10 日

0 投票

You can use mexCallMATLAB to call FEVAL with the function handle as argument.
Perhaps somebody knows how to use mxFevalFunctionHandle?

1 件のコメント

Bård Skaflestad
Bård Skaflestad 2011 年 11 月 11 日
Thank you, Jan. This is the solution I ended up using.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLanguage Fundamentals についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by