how can set values to a const mxArray ?

조회 수: 3 (최근 30일)
omid jab
omid jab 2013년 5월 6일
Hi all, Suppose defined a const mxArray *ptr[2]. I wanna set *ptr[0] to a string such as 'mystr', and set *ptr[1] to a int varible such as 10. how can I do it? for example help me to modify this code:
const mxArray *prhs[2]; char a = 'mystr'; prhs[0] = a; prhs[1] = 10;

채택된 답변

José-Luis
José-Luis 2013년 5월 6일
편집: José-Luis 2013년 5월 6일
Looks like you are talking about C/C++ code. If that is the case, I am confused. How could:
char a = 'mystr'
ever work? That should not work for one of two reasons: either a should be a pointer to a character array or mystr a single character.
That being said, there are some other issues with your code. You declare prhs to be a pointer to a const mxArray. Then you try to assign values to prh, something that you have never declared. Finally you are trying to assign a character and a numeric value to prh, which is a pointer to an mxArray. That's a no-no.
Anyway, knowing what errors you get and what you are actually trying to do would greatly facilitate the task of those trying to help you.
  댓글 수: 3
José-Luis
José-Luis 2013년 5월 6일
편집: José-Luis 2013년 5월 6일
You could use functions from the Matlab C++/Fortran API. Please look here and examples therein. For what you want to do, you should look in particular at:
doc mxCreateString
doc mxCreateDoubleScalar
I will not give you a working example because that would be doing the work for you, and this sounds a lot like homework. I, and others, will be more than happy to help you if you run into specific problems. For such a simple application however, you can pretty much copy/paste from some of the examples provided by the Mathworks.
omid jab
omid jab 2013년 5월 6일
thanks. these examples are very good.

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

추가 답변 (3개)

Jan
Jan 2013년 5월 6일
편집: Jan 2013년 5월 6일
mxArray *prhs[2] is a field of pointers to mxArrays. The elements must be pointers to mxArrays then, not a pointer to a string or even an integer value directly.
Even char a='mystr' will fail, because a must be a pointer to a char, when it should carry a string in C.
I assumed there is a confusion with the names prhs and prh.
These are such fundamental problems concerning C that I do not think, that a solution will really help. You have to understand the bascic of C at first, befaore you can use it securely. And afterwards you can include the functionality of Matlab arrays. Read the C-Mex examples shipped with Matlab carefully to learn more details.
  댓글 수: 4
James Tursa
James Tursa 2013년 5월 6일
편집: James Tursa 2013년 5월 6일
@omid jab: Here is the code. However, I am of the same opinion as Jan that these two lines may solve your stated problem in this post, but it is likely that they in and of themselves will not solve your overall problem, and that you likely need to learn C and go though the mex examples in the doc to know how to use these lines.
prhs[0] = mxCreateString("mystr");
prhs[1] = mxCreateDoubleScalar(10);
omid jab
omid jab 2013년 5월 6일
thanks, I have started learning C.

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


Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 6일
편집: Azzi Abdelmalek 2013년 5월 6일
prh{1}='mystr' % Matlab does not allow 0 as index (it must be logical or positive integer)
prh{2}=10
Look at cell array
  댓글 수: 2
omid jab
omid jab 2013년 5월 6일
That is a C code. for more information google "Mex-file".
Jan
Jan 2013년 5월 6일
편집: Jan 2013년 5월 6일
@omid jab: On one hand it might be obvious, that the shown code should be C (but it is only partially). On the other hand it is a good practice to mention in a Matlab forum explicitly, when you ask a question about another language.

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


omid jab
omid jab 2013년 5월 6일
@ José-Luis, Azzi Abdelmalek, and Jan Simon, thaks for your great guidances.

카테고리

Help CenterFile 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!

Translated by