Problem with set field by number

조회 수: 15 (최근 30일)
Joachim Stallmann
Joachim Stallmann 2013년 8월 1일
Hi
I am having a problem using mxSetFieldByNumber. I want to set fields in a N-D structure array. I have no problem with setting fields in a Nx1 structure array. With a Nx1 structure array index is set as a int and corresponds to the element I want to write data.
int index;
field_num = mxGetFieldNumber(pa, "field_name");
mxSetFieldByNumber(pa, index, field_num, new_value_pa);
But with a N-D structure array I cant seem to find a way to write data to the second column in the array. I if I Have a 6x2 structure array I can set data to the first column in the array by setting the index to 1...6. I tried to set index 7 but then the structure array is changed to a array with dimensions of 7x2.
What should I do to populate the second column of the array? Thank You Joachim

채택된 답변

Jan
Jan 2013년 8월 1일
편집: Jan 2013년 8월 1일
Using index=7 is the perfect method to set the [1 x 2] elements of a [6 x N] array, e.g. a struct array also. So please post a piece of code, which reproduces your problem. You cannot change the size of a (struct)-array inside C++ only by writing to a non-existent index. While this works in Matlab, it crashes the memory manager in C++. So perhaps all you see is a severe bug.
  댓글 수: 1
Joachim Stallmann
Joachim Stallmann 2013년 8월 2일
Hi
Thank you. While cleaning my code, to post it, I found my mistake. I created the structure twice, the last one as a [12x2] structure array, due to a wrong if statement. Thanks works now. Joachim

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

추가 답변 (1개)

Philip Borghesani
Philip Borghesani 2013년 8월 1일
If you are trying to grow an existing structure then you will need to reshape the structure when done. If you create the structure ahead of time with mxCreateStructArray then this will not happen. I hope you are not modifying a structure input to the mex function.
  댓글 수: 1
Joachim Stallmann
Joachim Stallmann 2013년 8월 1일
Hi
I am modifying a structure in a c++ program. Why is it not a good idea? It is possible to add fields to a structure dynamically, so I would anticipate that it should be possible to "fill" them.
I know the dimensions of the structure beforehand but not the number of fields:
mxCreateStructArray(ndim_known,dims_known,nfields_unknown,fieldnames_unknown)
I add fields to the structures as the inputs change. I have been told that from a memory point of view it is always better to define the size of the variable, but in this case I have no idea what it would be. I will have a look at the reshaping of the structure, I am new to this but I will have a look.
Thanks for your help, I appreciate it
Joachim

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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by