What should I replace 'mxGetName' with?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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
2017 年 11 月 1 日
編集済み: James Tursa
2017 年 11 月 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 件のコメント
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
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
2017 年 11 月 2 日
Ok. I'll give that a try. Thank again for the help
Joseph
2017 年 11 月 8 日
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
James Tursa
2017 年 11 月 8 日
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.
Joseph
2017 年 11 月 8 日
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
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
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?
Do you mean from this line?
mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] )
James Tursa
2017 年 11 月 9 日
Yes.
Joseph
2017 年 11 月 10 日
Well, I've done the following:
- Added the 'char *mxGetName(const mxArray *mx).....' to the top of 'irsimit.c'
- Changed 'int tx_dims;' and 'int rx_dims;' to 'const mwSize *tx_dims;' and 'const mwSize *rx_dims;'
- 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) についてさらに検索
製品
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
