Convert matlab struct to C struct for mex

3 次查看(过去 30 天)
I have a struct that I'm writing in C and need to use a mex function to use it in matlab. The struct was originally in matlab and need to have the variables:
force = feval(forcing, Euler, Euler.x(:,k));
q = squeeze(q(:,k,:));
q0 = squeeze(Euler.q0(:,k,:));
nx = Euler.nx(:,k);% normal vector
J = Euler.J(1,k);
rx = Euler.rx(1,k);
qh = reshape(qh(k:k+1,:),[],1);
k = k;
The mex function I have come up with is:
#include "mex.h"
/* Extract local info for element k */
/* local struct */
typedef struct
{
double force;
double q;
double q0;
double nx;
double J;
double rx;
double qh;
double k;
} LocalInfo;
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *field;
LocalInfo Local;
field = mexGetVariable(const char global,const char force);
Local.force = mxGetScalar(field);
field = mexGetVariable(const char global,const char q);
Local.q = mxGetScalar(field);
field = mexGetVariable(const char global,const char q0);
Local.q0 = mxGetScalar(field);
field = mexGetVariable(const char global,const char nx);
Local.nx = mxGetScalar(field);
field = mexGetVariable(const char global,const char J);
Local.J = mxGetScalar(field);
field = mexGetVariable(const char global,const char rx);
Local.rx = mxGetScalar(field);
field = mexGetVariable(const char global,const char qh);
Local.qh = mxGetScalar(field);
field = mexGetVariable(const char global,const char k);
Local.k = mxGetScalar(field);
}
I don't think I'm using the mexGetVariable right, but I haven't used it before and can't figure it out.
The forcing function in the matlab part is:
function [f] = forcing(Euler, x)
% Compute the forcing to yield exact solution
% $$$ rhoExact = @(x,t) 1 + A * sin(pi * (x - t));
% $$$ if (Euler.forcingFlag ~= 1
% $$$ uExact = @(x,t) ones(size(x));
% $$$ else
% $$$ uExact = @(x,t) x;
% $$$ end
% $$$ pExact = @(x,t) ones(size(x));
% $$$ EnerExact = @(x,t) pExact(x,t)/gamm1 + ...
% $$$ 0.5 * rhoExact(x,t) .* uExact(x,t).^2;
if Euler.forcingFlag == 1,
A = Euler.A;
t = Euler.time;
gamm1 = Euler.gamm1;
r = 1 + A * sin(pi * (x - t));
dr_t = -pi * A * cos(pi*(x-t));
dr_x = pi * A * cos(pi*(x-t));
u = x*1.e-4;
du_t = 0;
du_x = 1*1.e-4;
p = 1;
dp_t = 0;
dp_x = 0;
E = p / gamm1 + 0.5 * r .* u.^2;
dE_t = dp_t / gamm1 + 0.5 * dr_t .* u.^2 + r .* u .* du_t;
dE_x = dp_x / gamm1 + 0.5 * dr_x .* u.^2 + r .* u .* du_x;
fluxr = r .* u;
dfluxr_t = dr_t .* u + r .* du_t;
dfluxr_x = dr_x .* u + r .* du_x;
fluxru = r .* u.^2 + p;
dfluxru_x = dr_x .* u.^2 + 2 * r .* u .* du_x + dp_x;
fluxE = (E + p) .* u;
dfluxE_x = (dE_x + dp_x) .* u + (E + p) .* du_x;
f = zeros(size(x,1),3);
f(:,1) = dr_t + dfluxr_x;
f(:,2) = dfluxr_t + dfluxru_x;
f(:,3) = dE_t + dfluxE_x;
else
f = zeros(size(x,1),3);
end

回答(0 个)

类别

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