📄 sb2slmat.m
字号:
function sb2slmat(blkname,in,fl,cod,timer)
% SB2SLMAT converts user-defined FORTRAN blocks into MATLAB MEX-files.
% The MEX-file can be called directly by SIMULINK S-functions.
%
% SB2SLMAT(BLKNAME,IN,FL,COD,TIMER) converts a SystemBuild block
% to a SIMULINK block. BLKNAME is a string containing the name of
% the block. The parameters are set up for this block based on the
% SystemBuild file coding integer IN and real number FL.
%
% COD is a three dimensional vector. The first element indicates
% whether it is a continuous (0) or discrete (1) time system.
% The second element indicates whether it does not need (0) or
% does need (1) a mask. The third element indicates the
% orientation of the block.
%
% TIMER is designed for discrete time only. The first element of
% TIMER is the sampling time. The second element is the initial time.
% Wes Wang 11/92
% Copyright (c) 1990-93 by The MathWorks, Inc.
% $Revision: 1.12 $ $Date: 1993/11/21 21:29:17 $
% File sb2slmdt.m is called by this function.
% process the data
% parameter state: input, output, state, #real pm, #integer pm
xx = num2str(in(3));
if length(xx)<2
xx = ['0' xx];
end;
userA1 = ['usr' xx];
if in(17)+3 <= length(in)
pmi = in(in(17)+3:length(in))';
else
pmi = 0;
end;
temp = 1;
if cod(1) >0
dt = timer(1);
else
dt = 0.001;
end
if ~isempty(fl)
if in(in(14)+1) > 0
for i=1:in(in(14)+1)
j = in(in(in(14)+1)+in(14)+1+i);
ini(j) = fl(i);
end;
temp = in(in(14)+1) + 1;
else
ini = zeros(1,in(6));
end
% real parameter
j = length(fl) - in(6) - in(4) - 2*(in(4)+in(5)+in(6)) - temp + 1;
if j > 0
pmr = fl(temp: j+temp-1)';
temp = j+temp;
else
pmr = 0;
end;
% input linear delta
if length(fl) >= temp+in(4)-1
ild = fl(temp:temp+in(4)-1)';
temp = temp + in(4);
else
ild = 0.01*ones(in(4),1);
end
%state linear delta
if length(fl) >= temp+in(6)-1
sld = fl(temp:temp+in(6)-1);
else
sld = 0.01*ones(in(6),1);
end
else
pmr = 0;
ild = 0.01*ones(in(4),1);
sld = 0.01*ones(in(6),1);
ini = zeros(in(6),1);
end;
pms = [in(4) in(5) in(6)]; % length(pmi) length(pmr) cod(1) (in(in(14)) == 0)];
% the lat one is with/without direct term
% the parameter will be used are:
% pmi, pmr, pms, ini
% process the blocks
if in(4) > 1 | in(5) > 1
add_block('built-in/S-function',[blkname '/MATLAB Fcn'])
set_param([blkname '/MATLAB Fcn'],...
'position',[205,70,265,110])
tempi = 'MATLAB Fcn';
tempo = 'MATLAB Fcn';
if in(4) > 1
add_block('built-in/Mux',[blkname '/Mux'])
set_param([blkname '/Mux'],...
'inputs',num2str(in(4)),...
'position',[130,60,165,80+in(4)*15])
tempi = 'Mux';
autoline(blkname,'Mux/1','MATLAB Fcn/1');
end;
if in(5) > 1
add_block('built-in/Demux',[blkname,'/Demux'])
set_param([blkname '/Demux'],...
'outputs',num2str(in(5)),...
'position',[315,60,345,80+in(5)*15])
tempo = 'Demux';
drawnow
tmp = get_param([blkname '/Demux'],'position');
set_param([blkname '/Demux'],'position',tmp);
autoline(blkname,'MATLAB Fcn/1','Demux/1');
end;
for i=1:in(4)
set_param([blkname '/input' num2str(i)],...
'Port',num2str(i),'position',...
[30+40*rem(i-1,2),45+i*20,50+40*rem(i-1,2),65+i*20])
autoline(blkname,['input' num2str(i) '/1'],[tempi '/' num2str(i)]);
end;
for i=1:in(5)
set_param([blkname '/output' num2str(i)],...
'Port',num2str(i),'position',...
[430-40*rem(i-1,2),45+i*20,450-40*rem(i-1,2),65+i*20])
autoline(blkname,[tempo '/' num2str(i)],['output' num2str(i) '/1']);
end;
tempo = [blkname '/MATLAB Fcn'];
else
tempo = blkname;
end;
set_param(tempo,...
'function name',userA1,...
'parameters','pms,pmi,pmr,ini,dt');
%do mex files. To be continued.
set_param(blkname,'Mask Display',[userA1 '\nBlock'],...
'Mask Type','User Defined FORTRAN code','Mask Dialogue',...
'User Code Block.|number of [inputs outputs states]:|continuous(0)/discrete(1) time system:|integer parameters:|real parameters:|initial conditions:|with(1)/without(0) direct term:|sampling time for disct system:')
set_param(blkname,...
'Mask Translate','pms=@1;cd=@2;pmi=@3;pmr=@4;ini=@5;wwdt=@6;dt=@7;pms=[pms length(pmi) length(pmr) cd wwdt]','Mask Help',...
'This block is a MATLAB S-function MEX-file, translated from a SystemBuild User Code Block (FORTRAN file).')
set_param(blkname,'Mask Entries',[ mat2str(pms) '\/' num2str(cod(1)) '\/' mat2str(pmi) '\/' mat2str(pmr) '\/' mat2str(ini) '\/' num2str(in(in(14))==0) '\/' num2str(dt) '\/'])
sb2slmdt
userB1 = [userA1 'x.f'];
f_sb = fopen(userB1,'w');
fwrite(f_sb,x1);
x1 = [setstr(10) ...
' PARAMETER (NPMI=' num2str(length(pmi)) ') ' setstr(10) ...
' PARAMETER (NPMR=' num2str(length(pmr)) ') ' setstr(10) ...
' PARAMETER (NU = ' num2str(in(4)) ') ' setstr(10) ...
' PARAMETER (NY = ' num2str(max(in(5),in(6))) ')' setstr(10) ...
];
fwrite(f_sb,x1);
if in(6) > 0
x1 = [setstr(10) ...
' PARAMETER (NX = ' num2str(in(6)) ') ' setstr(10) ...
' REAL*8 X(NX), XDOT(NX), X0(NX) ' setstr(10) ...
];
fwrite(f_sb,x1);
else
x1 = [setstr(10) ...
' PARAMETER (NX = ' num2str(in(6)) ') ' setstr(10) ...
' REAL*8 X, XDOT, X0 ' setstr(10) ...
];
fwrite(f_sb,x1);
end;
fwrite(f_sb,x2);
x2 = [' EXTERNAL ' userA1 setstr(10)];
fwrite(f_sb,x2);
fwrite(f_sb,x3);
fwrite(f_sb,userA1);
fwrite(f_sb,x4);
fclose(f_sb);
clear x1 x2 x3 x4
x1 = computer;
if findstr(lower(x1),'vms')
eval(['!fortran ', userA1]);
eval(['!fortran ', userA1, 'x.f']);
x1 = ['!fmex ',userA1, ' ', userA1, 'x'];
else
if (x1(1:2) == 'PC') | (x1(1:2) == 'MA')
x1 = '!fmex ';
else
[x2,x3]=unix('which fmex4');
if isempty(x3)
x1 = '!fmex ';
else
x1 = '!fmex4 ';
end;
end;
x1 = [x1 userA1 '.f ' userB1];
end;
eval(x1)
%end of sb2slmat.m
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -