Mex mxGetField do loop

Hello,
I'm trying to use a do loop in my mex function that will take the values passed via a struct form from matlab. The matlab struct is as follows:
TurbineComponents.Blade(1).Position = ...
TurbineComponents.Blade(1).Orientation = ...
etc
I try the following as my do loop in the mex file:
Do J = 1, NumBl
Blade1_pr = mxGetField(prhs(ArgNum),1,'Blade(J)')
b1p_pr = mxGetField(Blade1_pr,1,'Position')
b1p_ptr = mxGetPr(b1p_pr)
b1o_pr = mxGetField(Blade1_pr,1,'Orientation')
b1o_ptr = mxGetPr(b1o_pr)
b1v_pr = mxGetField(Blade1_pr,1,'RotationVel')
b1v_ptr = mxGetPr(b1v_pr)
b1t_pr = mxGetField(Blade1_pr,1,'TranslationVel')
b1t_ptr = mxGetPr(b1t_pr)
call mxcopyptrtoreal8(b1p_ptr,TurbineComponents%Blade(J)%Position,size2)
call mxcopyptrtoreal8(b1o_ptr,TurbineComponents%Blade(J)%Orientation,size3)
call mxcopyptrtoreal8(b1v_ptr,TurbineComponents%Blade(J)%RotationVel,size2)
call mxcopyptrtoreal8(b1t_ptr,TurbineComponents%Blade(J)%TranslationVel,size2)
end Do
The problem is coming from the first line of code, "Blade(J)" I believe. Is there a way to work around the field name?

 채택된 답변

James Tursa
James Tursa 2013년 5월 13일

0 개 추천

Get Blade using the J as the index. E.g.,
Blade1_pr = mxGetField(prhs(ArgNum),J,'Blade')

댓글 수: 4

Chris
Chris 2013년 5월 13일
Thanks James
Chris
Chris 2013년 5월 13일
Hi James, Changing to J as the index number worked for when J=1 however when it tries to move to the next blade, "TurbineComponents.Blade(2).Position", etc.. then matlab shuts down from a "segmentation error". Is there something wrong with the struct naming? Does some kind of preallocation need to be done?
This would be expected if not all elements are pre-allocated. When you create a struct or cell array the actual values for the elements are NULL pointers until you set them to something. So if you are passing a struct to your mex function with only some of the elements set, then you need to check for this condition inside your mex function (actually it is always a good idea to check for this regardless of what you expect). E.g.,
Blade1_pr = mxGetField(prhs(ArgNum),J,'Blade')
if( Blade1_pr == 0 ) cycle ! or whatever action is appropriate
b1p_pr = mxGetField(Blade1_pr,1,'Position')
if( b1p_pr == 0 ) cycle ! or whatever action is appropriate
etc.
You will have to decide what action is appropriate when elements are not set. Maybe generate an error or maybe fill in with a default value, etc.
Chris
Chris 2013년 5월 14일
Thanks James

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Fortran with MATLAB에 대해 자세히 알아보기

질문:

2013년 5월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by