function busmux(outputbus,inputbuslist)
%BUSMUX Merges datastructures from multiple SIMULINK busses.
%
%Reads listed input bus files and outputs a third file which is a
%combination of the input files.
%William York, 12/95.
%Copyright (c) 1995 by The MathWorks, Inc.
%$Revision: 1.1 $ $Date: 1997/02/11 20:50:04 $
% disp('Entering busmux')
YES=1;
NO=0;
% Get the number of bus names in inputbuslist
[BusNames, Remainder] = strtok(inputbuslist, '|');
while ~isempty(Remainder)
[Name, Remainder] = strtok(Remainder, '|');
BusNames=str2mat(BusNames,Name);
end
NumBusNames=size(BusNames,1);
for (Ind=1:NumBusNames)
eval(['[str_bus' num2str(Ind) ',siz_bus' num2str(Ind) ',str]=busread(deblank(BusNames(Ind,:)));']);
if (isempty(eval(['siz_bus' num2str(Ind) ])))
errstr=str2mat(['Error loading input ' num2str(Ind) ' bus file:'],['- ' str]);
errordlg(errstr,[gcb ' Error']);
return;
end
end
[str_outputbus,siz_outputbus,str]=busread(outputbus);
if (isempty(str_outputbus))
if (exist(outputbus))
errstr=str2mat('Error loading output bus file:',['- ' str]);
errordlg(errstr,[gcb ' Error']);
return;
else
OutputbusExisted=NO;
end
else
OutputbusExisted=YES;
end
desired_str_outputbus=str2mat(str_bus1,str_bus2);
for (Ind=3:NumBusNames)
eval(['desired_str_outputbus=str2mat(desired_str_outputbus,str_bus' num2str(Ind) ');']);
end
desired_siz_outputbus=[siz_bus1; siz_bus2];
for (Ind=3:NumBusNames)
eval(['desired_siz_outputbus=[desired_siz_outputbus;siz_bus' num2str(Ind) '];']);
end
% Test or Create the outputbus file
if (OutputbusExisted==YES)
% Check if the file is a valid combination of bus1 and bus2 - this is what the autogeneration
% function would write out.
if (~strcmp(desired_str_outputbus,str_outputbus) | ~isequal(desired_siz_outputbus,siz_outputbus))
% Either the strings have been reordered or the bus file is not a valid combination of bus1 and bus2
% Recreate the file
disp(['Recreating file ' outputbus '.txt'])
delete([outputbus '.txt'])
RecreateOutputBus=YES;
else
RecreateOutputBus=NO;
end
end
if (OutputbusExisted==NO | RecreateOutputBus==YES)
% Build a new outputbus file
if (OutputbusExisted==NO)
% The file did not exist, so tell the user that a new one will be created
QuestString=str2mat(['WARNING: File ' outputbus '.txt does not exist on the MATLAB path.'], ...
' Click Create to create the file in the current directory automatically.', ...
' Click Cancel to abort and create the file by hand.');
Response=questdlg(QuestString,'Create','Cancel');
if (strcmp(Response,'Cancel'))
return
end
end
% Create a new file
fid_outputbus=fopen([outputbus '.txt'],'wt');
if (fid_outputbus < 0)
error(['Can''t open file ' outputbus '.txt'])
end
% The bus file either did not exist or was not valid so create the bus file
fprintf(fid_outputbus,'%%This file is a plain text definition for DataBus %s.\n',outputbus);
fprintf(fid_outputbus,'%%\n');
fprintf(fid_outputbus,'%%Type "help simbus" for more information.\n');
fprintf(fid_outputbus,'\n');
[NumSignals,MaxStringWidth]=size(desired_str_outputbus);
fprintf(fid_outputbus,['%%%-' num2str(MaxStringWidth) 's%s\n'],'Name','Size');
for (Ind=1:NumSignals)
fprintf(fid_outputbus,['%-' num2str(MaxStringWidth) 's %d\n'],deblank(desired_str_outputbus(Ind,:)),desired_siz_outputbus(Ind));
end
% Close the output file
if (fclose(fid_outputbus) < 0)
error(['Can''t close file ' outputbus '.txt'])
end
end
% disp('Leaving busmux')
% EOF busmux.m