www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/ShrlibPreconvertMATLABStructureBeforeAddingValuesExample.m

    %% Preconvert MATLAB Structure Before Adding Values
% This example shows how to preconvert structure |sm| to |c_struct| before calling  
% |addStructFields|. If you repeatedly pass |sm| to functions,
% preconverting eliminates the processing time required by MATLAB to autoconvert the
% structure for each function call.
% 
% Create and initialize a MATLAB structure.

% Copyright 2015 The MathWorks, Inc.

sm.p1 = 476;
sm.p2 = -299;
sm.p3 = 1000;
%%
% Load the library containing the |addStructFields| function.
if not(libisloaded('shrlibsample'))
    addpath(fullfile(matlabroot,'extern','examples','shrlib'))
    loadlibrary('shrlibsample')
end
%% 
% Convert the fields, which are of type |double|, to match the |c_struct| 
% structure types, |double|, |short|, and |long|.
sc = libstruct('c_struct',sm);
%% 
% Display the field names and values.
get(sc)
%% 
% Add the field values.
calllib('shrlibsample','addStructFields',sc)