I am working with this code and can't compile it because of the deprecated 'mxGetName'.
the line is:
temp_nrhs=7;
if((nrhs>=8)&&(!strcmp(mxGetName(prhs[temp_nrhs]),"things")))
and simply removing it results in a warning
warning: passing argument 1 of 'strcmp' from incompatible pointer type
I'm not sure what can I replace it with.

 채택된 답변

James Tursa
James Tursa 2017년 11월 1일
편집: James Tursa 2017년 11월 1일

1 개 추천

Variable name strings used to be a part of the mxArray itself. However, that hasn't been the case for several years now. There is no easy way to recover the name of the passed variable other than to use mexCallMATLAB with "whos" and do an exhaustive search for the variable mxArray address to discover its name in the caller's workspace. So, you can either do that, or just drop that part of the test completely out of your mex code.
EDIT
If you really think you need this functionality, you could use the following function. The difference for this function compared to the original is that this function returns a pointer to dynamically allocated memory, so you might want to mxFree( ) it (or allow MATLAB to garbage collect it).
char *mxGetName(const mxArray *mx)
{
mxArray *lhs[1];
mxArray *cell;
size_t i, n;
char *varname;
varname = (char *) mxMalloc(65);
if( mx ) {
mexCallMATLAB(1,lhs,0,NULL,"who");
n = mxGetNumberOfElements(lhs[0]);
for( i=0; i<n; i++ ) {
cell = mxGetCell(lhs[0], i);
mxGetString(cell, varname, 65);
if( mx == mexGetVariablePtr("caller", varname) ) {
mxDestroyArray(lhs[0]);
return varname;
}
}
mxDestroyArray(lhs[0]);
}
varname[0] = '\0';
return varname;
}

댓글 수: 11

Joseph
Joseph 2017년 11월 1일
편집: Joseph 2017년 11월 1일
Hi James, thanks for the quick answer.
Unfortunately, I can't drop it out from the code as it is used 3 times in the code.
I was wondering if the error in the console could provide any indication on what to use.
So after replacing 'mxGetName' with 'matGetVariable'
warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast
if((nrhs>=8)&&(!strcmp(matGetVariable(prhs[temp_nrhs]),"things"))){
^
.
.
.
note: expected 'const char *' but argument is of type 'int'
int __cdecl strcmp(const char *_Str1,const char *_Str2);
Maybe the ' expected 'const char *'' might point to something? or will it require mexCallMATLAB with "whos" like you said?
If that's the case could you please provide any link that can assist me?
Thanks again for the help
James Tursa
James Tursa 2017년 11월 1일
편집: James Tursa 2017년 11월 1일
You do not want to replace mxGetName with matGetVariable ... these functions do entirely different things.
If you don't want to change your source code, then one way to essentially disable that test simply is to add this function yourself. E.g., add this into your source code:
char things[] = "things"; /* at the top level, not inside a function */
char *mxGetName(const mxArray *mx)
{
return things;
}
This will force your code to always pass that part of the test, essentially disabling it.
If you for some reason really need to check to see if the variable passed in has the name "things" in the caller's workspace let me know (although I don't see why this would be important).
Joseph
Joseph 2017년 11월 2일
Ok. I'll give that a try. Thank again for the help
Hi James, sorry to prolong this again.
So I placed the function you wrote (the one from the first edited answer) at the top of the code and run it. Now the warning becomes:
warning: assignment from incompatible pointer type
tx_dims = mxGetDimensions(prhs[temp_nrhs]);
And the other warning is:
warning: incompatible implicit declaration of built-in function 'free'
free(start);
I haven't used mxFree() anywhere
You haven't posted the definitions of "tx_dims" and "start", and how "start" is assigned a value. For "tx_dims", it should be:
const mwSize *tx_dims;
I suspect you are using something other than mwSize, like int, in your current code.
For "start", it would need to be assigned a value from one of the native C/C++ memory allocation functions (malloc, calloc, realloc) in order to use free( ). If "start" were assigned a value from one of the MATLAB API memory allocation functions (mxMalloc, mxCalloc, mxRealloc), then you would need to use mxFree( ). I can't advise you definitely in either of these cases since you haven't posted the relevant code for me to see, so I can't tell for sure what the problem is.
You're right, the code uses 'int', and changing to 'mwSize' leads to:
warning: incompatible implicit declaration of built-in function 'malloc'
Basically, I thought I'll bring the code into the modern era as it is very useful for students and researchers and will save time instead of replicating the whole thing again.
But the issues from the 3 mentions of 'mxGetName' in 'irsimit.c' makes it seem like it's not worth it.
James Tursa
James Tursa 2017년 11월 8일
편집: James Tursa 2017년 11월 8일
mxGetName( ) is not the problem. That issue is solved. You likely need to include the appropriate header for malloc:
#include <stdlib.h>
James Tursa
James Tursa 2017년 11월 9일
And I guess I have been assuming all of this time that the "prhs" in your code is the one from the mexFunction argument list. Is this true?
Joseph
Joseph 2017년 11월 9일
편집: Joseph 2017년 11월 9일
Do you mean from this line?
mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] )
James Tursa
James Tursa 2017년 11월 9일
Yes.
Well, I've done the following:
  1. Added the 'char *mxGetName(const mxArray *mx).....' to the top of 'irsimit.c'
  2. Changed 'int tx_dims;' and 'int rx_dims;' to 'const mwSize *tx_dims;' and 'const mwSize *rx_dims;'
  3. Added '#include <stdlib.h>' to 'calculateIt.c'
And now the only thing that comes up is:
mex irsimit.c calculateIt.c planes.c boxes.c visibility.c
Building with 'MinGW64 Compiler (C)'.
MEX completed successfully.
Thanks a lot for the help James, it was beyond helpful :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

제품

질문:

2017년 11월 1일

댓글:

2017년 11월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by