⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matlab2fmex.m

📁 student teacher and proferssor llove this project,this a very important exmple
💻 M
📖 第 1 页 / 共 5 页
字号:
function [out,maxmxinputs,maxmxinputs0,needed_interfaces,libraries,suborfun]=matlab2fmex(filename_al,tempflagin_al,varargin)% matlab2fmex(filename,tempflag,varargin)%    There are 2 steps required to convert a Matlab m-file to a mex-able %Fortran90 file using matlab2fmex.%%    1) matlab2fmex needs to find a *.mat data file which contains a %typical workspace generated by the function which is being converted. %This is easily constructed by inserting a keyboard command at the end %of the *.m file, then, at the k>> promt simply type "save *" where * %is the name of the *.m file without the .m extension. %    matlab2fmex uses this workspace to make decisions which depend on %the variables in the *.m file, such as real or complex. Accordingly, if %an input variable may be complex, it is best to submit a complex input %when the workspace is saved so that the *.f90 file will be as robust as %possible.%    When converting multiple *.m files (a main function and helper%functions), a separate *.mat file must exist for each function.%    2) The second step is to call matlab2fmex. The format is the following:%matlab2fmex(filename,[options],{'first' 0or1},{'second' 0or1},...,%                      ['local',{'first_l' 'input_l'},...]);%%After the filename to be converted (with no .m extension), there must %be a cell such as {'first' 0or1} for _each_ output variable. Each output %variable (in order) is the same size as as the input variable 'first' %(input as a string). This tells the converter what size to make the output %as a function of the input. If your function does not have an input %function which is the same size as a given output, use a dummy input%variable specified with zeros(m,n). %    The second part of each cell specifies whether that output should%be declared complex (1) or not (0). If there is a chance it may become%complex, it is best to specify it as a 1. For multiple function conversion,%input a cell array for each output variable in order: e.g. %{{'first_of_fun1' 0or1} {'first_of_fun2' 0or1}}%then a comma and the next output variable (if any). For an complete example,%see below.%%    Optionally, the user may specify the size of the local variables to%be the same size as any of the input variables. After the cell%specifications for the output variables are given, the string 'local'%is followed by a cell array specifying first which local variable you%want to assign, in this case 'first_l', and then by the input variable%name this local variable should be the same size as; in this case%'input_l'. See the EXAMPLES section for more information. If the size%of the local variables is not specified in this way, the converter%looks for input variables which happen to be the same size as each%local variables. That local variable is then assigned the size of that%input variable. Upon failing to find an input variable wiht the same%size, the converted declares that local variable to be the _static_%size it is is the saved workspace.%%    Some example calls are:%matlab2fmex('myfunc');%matlab2fmex('fibocon');%matlab2fmex('gasket');%etc.declare_globalsfilename_all=filename_al;if nargin>1,tempflagin_all=tempflagin_al;else tempflagin_al=[]; tempflagin_all=[];end%There are a few parameters which the user can adjust as well:%These can be input as flags in the function call. %    The default behaviour is below:%Converter Parameters%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%want_kb=0;%want_kb --> 0 Do not stop after each step, compile file. (default)                  %--> 1 Enter Matlab keyboard after each step.(for debugging)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%want_ss=0;%want_ss%-->-1 No subscript vectorization. (for mostly scalar operations)                  %--> 0 Relaxed subscript interpreting. (default)                  %--> 1 Stricter subscripting (slower, for variable subscripts)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%want_op=0;%want_op%-->-1 No operator replacement (use when want_ss=-1)                  %--> 0 Don't replace +, -, or scalar * and / operators. (default)                  %--> 1 Replace all math operators with interface calls. (slower)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%want_fb=1;%want_fb --> 0 Don't display progress throughout compilation. (default)                  %--> 1 Display feedback information. (potentially annoying)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%want_ct=1;%want_ct --> 0 Don't mex *.f90 upon completion.                  %--> 1 mex the resulting *.f90 file. (default)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%want_0=0;%want_0   --> 0 Don't zero all vars (default                  %--> 1 Set all vars = 0 before computational routine%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%want_br=0;%want_br --> 0 Use (/ and /) instead of [ and ] (all Fortran 90's)                  %--> 1 Use [ and ] (Some Fortran 90's)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%want_sb=0;%want_sb --> 0 Convert as a full mex-file                  %--> 1 Convert as a subroutine (sets want_ct to 0).		  %--> 2 Convert as a function (one output, sets want_ct to 0).%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%want_in=1;%want_in --> 0 Don't allow local vars to be integers                  %--> 1 Allow local vars to be integers (default)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%want_cs=0;%want_cs --> 0 Assume all sqrt's are of real, positive numbers                  %--> 1 Assume all sqrt's are complex%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%See README file for further documentation%%%try% We need to parse varargin for output specs, possible local and input specsif nargin<2, tempflagin_all=[];end[vararginout,vararginin,vararginlocal]=parsevarargin(varargin);if iscell(filename_al), filename=filename_al{1}; else filename=filename_al; endif iscell(tempflagin_all), tempflagin=tempflagin_all{1}; else tempflagin=tempflagin_all; endvararginout_all=vararginout;if length(vararginout)>0 if iscell(vararginout{1}{1})  for i=1:size(vararginout_all,2)   if iscell(vararginout{i}{1})    vararginout{i}=vararginout_all{i}{1};   else    vararginout{i}=vararginout_all{i};   end  end endendif iscell(filename_al) suborfun=ones(max(size(filename_al)),1); suborfun(1)=0;else if want_sb~=0  suborfun=want_sb-1; else  suborfun=0; endendif exist([filename,'.mat'],'file') cw=load(filename);else error(['Couldn''t find ',filename,'.mat workspace file!']);endif ~isempty(tempflagin) for i=1:length(tempflagin)  switch i   case 1    want_kb=tempflagin(1);   case 2    want_ss=tempflagin(2);   case 3    want_op=tempflagin(3);   case 4    want_fb=tempflagin(4);   case 5    want_ct=tempflagin(5);   case 6    want_0=tempflagin(6);   case 7    want_br=tempflagin(7);   case 8    want_sb=tempflagin(8);    if want_sb~=0     disp('Making subroutine');     want_ct=0;    end   case 9    want_in=tempflagin(9);   case 10    want_cs=tempflagin(10);  end endendfor i=length(tempflagin)+1:11 switch i  case 1   tempflagin(1)=want_kb;  case 2   tempflagin(2)=want_ss;  case 3   tempflagin(3)=want_op;  case 4   tempflagin(4)=want_fb;  case 5   tempflagin(5)=want_ct;  case 6   tempflagin(6)=want_0;  case 7   tempflagin(7)=want_br;  case 8   tempflagin(8)=want_sb;  case 9   tempflagin(9)=want_in;  case 10   tempflagin(10)=want_cs; endendif want_sb==0 disp(['Converting --- ',filename,'.m  ==>  ',filename,'.f90  ==>  ',filename,'.mex']);else disp(['Converting --- ',filename,'.m  ==(added to)==>  mexfunctions.f90']);end%%%catch%%% error(['Couldn''t load the workspace file for ',filename,'.m. Exiting...'])%%%end% Load keywords and function words.keywords={'case';'select';'else';'elseif';'for';'function';'if';'otherwise';'profile';'switch';'while';'call';'then';'and';'or';'not';'inf';'endif';'do';'enddo';'exit'};keywordsbegin={'for';'while';'switch';'if';'do';'elseif';'case'};operators={'.''' 'mxtr';'''' 'mxctr';'.^' 'mxdpow';'^' 'mxpow';'*' 'matmul';'/' 'mxdiv';'.*' 'mxdmult';'./' 'mxddiv';'+' 'mxplus';'-' 'mxsub'};logicalops={'~' '.not.';'&' '.and.';'|' '.or.'};loglist={'~';'==',;'&';'|';'>';'<';'xor';'any';'all';'isnan';'isinf';'isfinite'};mxwords={'mxGetM';'mxGetN';'mxCreateFull';'mxGetPr';'mxGetPi';'mxSetM';'mxSetN';'mexCallMATLAB';'mxs';'mxi';'mlcall';'mlcall0';'%val';'real';'aimag';'cmplx';'rhs';'lhs';'allocate';'deallocate';'num2str';'mxTR1';'mxTR2';'mxTR3';'mxTR4';'mxTR5';'mxTR6';'mxTR7';'mxTR8';'mxTR9';'mxTC1';'mxTC2';'mxTC3';'mxTC4';'mxTC5';'mxTC6';'mxTC7';'mxTC8';'mxTC9';'mxTI1';'mxTI2';'mxTI3';'mxTI4';'mxTI5';'mxTI6';'mxTI7';'mxTI8';'mxTI9'};for i=1:size(operators,1), mxwords{size(mxwords,1)+1}=operators{i,2}; endif ~want_sb,needed_interfaces=cell(1,2);libraries=' ';endfunwords=getfunwordsmlonly;mexoperatorinterfaces=getmexoperators;mexfunctions='';r=char(10);b=char(8);needpi=0;needeps=0;maxTRC=[0 0 0];typs{1}={'r';'c';'i';'l'};                     %General 2-D arraystyps{3}={'c';'t';'x';'d';'m'};                 %Complextyps{4}={'w';'x';'y';'z'};                     %1-D arrays in fortrantyps{5}={'s';'t';'u';'v'};                     %scalartyps{6}={'r';'s';'w';'e';'n'};                 %realtyps{7}={'i';'u';'y';'f';'o'};                 %integertyps{8}={'l';'v';'z';'g';'p'};                 %logicaltyps{9}={'d';'e';'f';'g'};                     %2-D row vectorstyps{10}={'m';'n';'o';'p'};                    %2-D column vectorstyps{11}={typs{1}{:},typs{9}{:},typs{10}{:}};  %Any 2-D arraytyps{12}={typs{11}{:},typs{4}{:}};             %non scalartyps{2}={typs{6}{:},typs{3}{:}};               %All but integers and logicalstyps{13}={'c';'d';'m'};                        %2-D complextyps{14}={'r';'e';'n'};                        %2-D realtyps{15}={'i';'f';'o'};                        %2-D integertyps{16}={'l';'g';'p'};                        %2-D logicaltyps{17}={'.';'e';'E';'d';'D'};                        %2-D logicalfortranfunwords={'dble';'aimag';'do';'enddo';'endif';'int';'nint';'iji';'conjg';'minval';'minloc';'maxval';'maxloc';'spread';'shape';'ubound';'product'};make_words={'dot1f','dot2f','sizef','lengthf','sumf','zerosf','onesf','minf','maxf','prodf','reshapef','repmatf','linspacef','diagf','normf','eigf','svdf','besseljf','besselyf','besselif','besselkf','besselhf','airyf','gammaf','gammalnf','gammaincf','medianf','meanf','acosf','acoshf','acotf','acothf','acscf','acschf','asecf','asechf','asinf','asinhf','atanf','atanhf','cothf','cschf','sechf','findf','fliplrf','flipudf','isnanf','ss2inf','isinff'};make_words2={'dot1','dot2','length','zeros','ones','prod','repmat','linspace','diag','norm','eig','svd','besselj','bessely','besseli','besselk','besselh','airy','gamma','gammaln','gammainc','median','mean','acosh','acot','acoth','acsc','acsch','asec','asech','asinh','atanh','coth','csch','sech','find','fliplr','flipud','ss2in','isinf'};intrinsics={'abs','acos','aimag','aint','all','anint','any','asin','atan','atan2','ceiling','cmplx','conjg','cos','cosh','cotan','count','dble','dot_product','exit','exp','floor','huge','int','isnan','log','log10','logical','matmul','max','maxloc','maxval','min','minloc','minval','mod','modulo','nint','not','pack','product','real','reshape','shape','sign','sin','sinh','size','spread','sqrt','sum','tan','tanh','tiny','transpose','ubound','unpack','mxs','mxi','dot_product'};barr={'c','r','i','l';'x','w','y','z';'t','s','u','v'}';barr2={'d','e','f','g';'m','n','o','p'}';alpha=isalpha; zzz=0;ticker={'m','a','t','l','a','b','2','f','m','e','x','.','m'};explicitinterfaces={'acosh','acoth','acsch','asech','asinh','atanh','coth','csch','log2','rem','sech','find'};% First read the function into funstr.filenamem=[filename,'.m'];funstr=cell(1,1);if exist(filenamem)==2 fid=fopen(filenamem); filestr=fscanf(fid,'%c'); fclose(fid); if ~strcmp(filestr(length(filestr)),r), filestr=[filestr,r]; end rets=findstr(r,filestr); rets=[0 rets]; temp=findstr(';',filestr); for i=1:length(temp)  temp3=rets(rets>temp(i));temp3=temp3(1);  goonimag=rets(rets<temp(i));goonimag=goonimag(length(goonimag));  if length(find(~isspace(filestr(temp(i)+1:temp3-1))))>0   goon(1)=length(findstr('[',filestr(temp(i)+1:temp3-1)));   goon(2)=length(findstr(']',filestr(temp(i)+1:temp3-1)));   if ~(goon(1)<goon(2))    if length(findstr('%',filestr(goonimag+1:temp(i)-1)))==0     filestr(temp(i))=r;    end   end  end end rets=findstr(r,filestr); rets=[0 rets]; count=1; temp2=''; for i=1:length(rets)-1  tempstr=[temp2,filestr(1+rets(i):rets(i+1)-1)];  if length(find(~isspace(tempstr)))>0   temp3=find(~isspace(tempstr));   if ((tempstr(1)~=r)&(tempstr(temp3(1))~='%'))    goon=1;    if length(findstr('...',tempstr))>0, goon=0; end    temp3=tempstr(~isspace(tempstr));    if ((strncmp(temp3,'disp',4))|(strncmp(temp3,'error',5))), goon=1; end    if goon     funstr{count}=tempstr;     temp1=findstr('%',funstr{count});     if ~isempty(temp1)      funstr{count}=funstr{count}(1:temp1(1)-1);     end     temp2='';     count=count+1;    else     temp=findstr('...',tempstr);temp=temp(1);     temp2=[tempstr(1:(temp-1))];    end   end  end endelse error(['I can''t find the file ',filenamem,'...']);endfunstr=funstr';s=size(funstr,1);if want_kb|want_fb disp([r,'Original:']); if s<20  showall(funstr) else  showall(funstr(1:20),1);  disp(['   . . .']) end disp(' ')endif want_kb,disp('Just read the function in'); showall(funstr), keyboard, end% Deblank. Find the words and numbers with start and endpointsfor i=1:s%Misc tasks% ------------ Insert -- RJHP 27Nov01 ----------------- funstr{i}(findstr(char(13),funstr{i}))=[]; %Remove CRs% -----------------------------------------------------%Deblank front and back funstr{i}=deblank(funstr{i}); funstr{i}=fliplr(deblank(fliplr(funstr{i})));%Ensure semilcolon at end if funstr{i}(end)~=';', funstr{i}=[funstr{i} ';']; endendupdatefunstr;% OK;now find the inputs, outputs, and other vars to the function

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -