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

📄 readsb.m

📁 数字通信第四版原书的例程
💻 M
📖 第 1 页 / 共 3 页
字号:
function err = readsb(sbfile,slfile,topblk)
%READSB translates ASCII SystemBuild files into SIMULINK.
%
%       READSB('SBFILE') translates the SystemBuild file SBFILE into 
%	SIMULINK and opens it in a SIMULINK window.  If there are more
%	than one superblocks in the file, you will be prompted to select
%	one or more superblocks to be translated. All of the
%	superblocks selected will be translated.
%
%       READSB('SBFILE','SLFILE') translates the SystemBuild file SBFILE
%       into a SIMULINK M-file SLFILE.M.
%
%       READSB('SBFILE','SLFILE','SUPERBLOCK') translates the
%       the SystemBuild subsystem SUPERBLOCK contained in SBFILE into
%	SIMULINK.  Subsystems contained in SUPERBLOCK are also translated.
%       The subsystem is saved in the file SLFILE.M.
%
%       ERR = READSB('SBFILE',...) returns a 1 if the translation
%       produces an error. A zero is returned if the translation is
%       successful.
%
%	Run LOAD SLFILE to load data if there is any data saved in the 
%	SystemBuild file and/or in the conversion.

%	Wes Wang 8/26/92 - 1/17/93
%	Copyright (c) 1990-94 by The MathWorks, Inc.
%	$Revision: 1.45 $  $Date: 1994/09/16 01:09:29 $

% This function is really lengthy; however, the data flow and functionality
% in each section are clear. The reason for not dividing this function
% into smaller functions is to avoid passing too many variables
% between functions and to avoid leaving a file open between functions.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Routine check up and make up for input variables                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Check if at least one parameter exists. If not, give error message.
err = 0;

sim_ver = 0;
if exist('bdclose')
    sim_ver = 1.3;
end;

% if there is no parameter input, error
if nargin <1,
  error('Usage: READSB SystemBuildFile');
end;

% if file to be saved is not specified
save_flag = 1;
if nargin <2 
  slfile = sbfile;
  save_flag = 0;
end;

% Check if slfile is empty. If it is empty, convert it from sbfile
if isempty(slfile)
  slfile = sbfile;
  save_flag = 0;
end;

% when top block is not specified
if nargin < 3
   topblk = [];
end

% make sure slfile is a row vector
[i,j]=size(slfile);
if i>j, 
  slfile = slfile'; 
end;

% take out the path indicated by '/' from SIMULINK file
ind = find(slfile == '/');
if ~isempty(ind)
  slfile = slfile(ind(length(ind))+1:length(slfile));
end;

% if it is a VMS computer, remove the header in a different way.

if findstr(lower(computer),'vms')
  ind = find(slfile == ']');
  if ~isempty(ind)
    slfile = slfile(ind(length(ind))+1:length(slfile));
  end;
end;

% add extension for slfile as *.m
ind = find(slfile == '.');

if isempty(ind)
 slstring = slfile;
 slfile = [slfile '.m'];
else
 slstring = slfile(1:min(ind)-1);
 slfile = [slstring,'.m'];
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read SystemBuild file                                                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% open SB file; and search for the necessary information.
f_sb = fopen(sbfile,'r');
if f_sb < 0
  error(['Cannot Open File ''' sbfile '''. Please Check Your Filename.'])
  return;
end;
x = fread(f_sb, 20, 'char');
tmp = find(x == 77);
if isempty(tmp)
  fclose(f_sb)
  error('Your file is not a SystemBuild File')
  return;
end;
flag_bi = 0;
if tmp(1) > 1
  x = fread(f_sb, tmp(1)-1, 'char');
  flag_bi = 1;
end;

%number of variables to be read in
numz = fscanf(f_sb,'%d',1);
xn  = 0;

x = fread(f_sb, 21, 'char');

fprintf('Your SystemBuild file was saved on')
fprintf(setstr(x(4:21)'));
fprintf('\n')

if flag_bi == 1
  fclose(f_sb)
  fprintf('We are sorry for cannot transfer your binary file yet.\n');
  return;
  numz = 0;
end;

% prepare files to be saved
save sb2sltmp slstring numz xn

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read and Save Data Variables						%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if numz > 0
  fprintf('Reading and Saving variables.')
  x = fread(f_sb, numz*20+ceil(numz/4), 'char');
  ind = find(x == 10);
  for i = length(ind):-1:1
    x(ind(i)) = [];
  end;
  for i=1:numz
    var_n = [var_n; setstr(x((i-1)*20+1:(i-1)*20+11)')];
    var_r = [var_r; str2num(setstr(x((i-1)*20+12:(i-1)*20+15))')];
    var_c = [var_c; str2num(setstr(x((i-1)*20+16:(i-1)*20+20))')];
  end;
  zzz = 'sb2sltmp';
  for i=1:numz
    fprintf('.');
    x = fread(f_sb, 40, 'char');
    x = fscanf(f_sb, '%e', var_r(i)*var_c(i));
    y = [];
    for j=1:var_c(i)
      tmp = (j-1)*var_r(i)+1:j*var_r(i);
      if length(x) >= max(tmp)
        y=[y,x(tmp)];
      end;
    end;
    x = deblank(var_n(i,:));
    appsave(zzz, x, y);
  end;
  disp([ num2str(numz) ' variables saved in the file.'])
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%find the location of the SystemBuild mark				%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('Reading and sorting SystemBuild block data')
x = fseek(f_sb, 0, -1);
x = fread(f_sb, inf, 'char');

%bug fix. if both 10 and 13 exist, take off both.
adj_length = 0;
ind = find(x==10);
if ~isempty(ind)
    adj_length = adj_length + 1;
end;
ind = find(x==13);
if ~isempty(ind)
    adj_length = adj_length + 1;
end;
%find where the System Build begins. It always begin with "System Build 2XX"
pos = findstr(x','SYSTEM BUILD ');
if isempty(pos)
  fclose(f_sb);
  error([sbfile ' is not a SystemBuild ASCII file.']);
else
  i = fseek(f_sb, pos(1)-1, 'bof');
  ver_num = fread(f_sb, 26, 'char');
  disp(' ');
  disp(['Your file was saved under ', setstr(ver_num')]);
  pos = pos(1) + 46;
end;

%find where the position for reading
i = fseek(f_sb, pos, 'bof');

% error information if anything goes wrong
if pos == 0 | i == -1,
  fclose(f_sb);
  error([sbfile ' is not a SystemBuild ASCII file.']);
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                      Read and Assign Variables						%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gen_block = fscanf(f_sb,'%d',4);

if gen_block(1) <= 0 
  disp('there is no block to be converted')
else
 %read the description for every block characters + four integers
 % (+ two real if the first integer is not zero)
 for i = 1:gen_block(1),
  tmp = fread(f_sb, 38, 'char');
  ind = find(tmp == 10);
  if ~isempty(ind)
     for i_tmp=length(ind):-1:1
        tmp(ind(i_tmp)) = [];
        tmp = [tmp; 32];
     end;
  end;
  ind = find(tmp == 13);
  if ~isempty(ind)
     for i_tmp=length(ind):-1:1
        tmp(ind(i_tmp)) = [];
        tmp = [tmp; 32];
     end;
  end;
  name_st = [name_st; tmp'];
  name_in = [name_in; fscanf(f_sb,'%d',4)'];
  if name_in(i,1) ~= 0
    name_fl = [name_fl; fscanf(f_sb, '%e', 2)'];
  else
    name_fl = [name_fl; [0 0]];
  end;
 end;
 %remove the extra length
 name_st=name_st(:,1:37);

 %read the content of each block.
 for i = 1:gen_block(1),
  % read the first line block information and four integer
  blk_wt = fread(f_sb,37+adj_length,'char')';
  ind = find(blk_wt == 10);
  if ~isempty(ind)
     for i_tmp=length(ind):-1:1
        blk_wt(ind(i_tmp)) = [];
        blk_wt = [blk_wt 32];
     end;
  end;
  ind = find(blk_wt == 13);
  if ~isempty(ind)
     for i_tmp=length(ind):-1:1
        blk_wt(ind(i_tmp)) = [];
        blk_wt = [blk_wt 32];
     end;
  end;
  if max(blk_wt(1:37) ~= name_st(i,:))
    error('Something goes wrong in the file. Abort Convertion');
  end;
  tmp = fscanf(f_sb,'%d',4);
  blk_dm = [blk_dm; tmp'];
  % read string with length blk_dm(i,1)
  fseek(f_sb,1,0);
  adj = adj_length*(floor(blk_dm(i,1)/72) + 1) - 1; %includes number for carriage return
  if isempty(adj)
    adj = 0;
  end;
  if findstr(lower(computer),'vms')
    tmp = fread(f_sb, 1, 'char');
  end;
  tmp = fread(f_sb, blk_dm(i, 1)+adj, 'char');
  ind = find(tmp == 10);
  if ~isempty(ind)
     for i_tmp=length(ind):-1:1
        tmp(ind(i_tmp)) = [];
     end;
  end;
  ind = find(tmp == 13);
  if ~isempty(ind)
     for i_tmp=length(ind):-1:1
        tmp(ind(i_tmp)) = [];
     end;
  end;
  blk_dm(i,1) = length(tmp);
  blk_st = [blk_st; tmp];
  % read integer with length blk_dm(i,2)
  blk_in = [blk_in; fscanf(f_sb, '%d', blk_dm(i,2))];
  % read real with length blk_dm(i,3)
  blk_fl = [blk_fl; fscanf(f_sb, '%e', blk_dm(i,3))];
 end;
end;
fclose(f_sb);
%xn=0;
%xs='save sb2sltmp xn xs';
%eval(xs);

fprintf('.');
if gen_block(1) > 0 
 %Remove all returns in the string
 ind = find(blk_st == 10);
 for i = length(ind):-1:1
   blk_st(ind(i)) = [];
 end;
 clear x i j ind

 % in the case of the top block is not a superblock in the file
 test=1;
 for i=1:gen_block(1)
     k=strcmp(topblk,deblank(setstr(name_st(i,:))));
     if k 
       test = 0;
     end;
 end;

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %Find the structure of the superblock tree 						%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 if test
   if gen_block(1) > 10
     disp(['The sorting is slow for your system with ' num2str(gen_block(1)) ' superblocks.'])
     disp('To speed up, input top superblock name next time.')
     disp('Type Ctrl-C to abort. Please wait.');
   end;
   top_blk = zeros(gen_block(1),1);
   %top_blk(i) == 0 if block_i is an independent superblock 
   %           == 1 if block_i is a subsuperblock
   sub_blk = zeros(gen_block(1),gen_block(1));
   %top_blk(i,:) lists all blocks depending on block_i
   ini_st = 1;
   %searching for the structure
   for i=1:gen_block(1)
     fprintf('.');
     if i ~= 1
       ini_st = sum(blk_dm(1:i-1,1))+1;
     end;
     st = setstr(blk_st(ini_st:ini_st+blk_dm(i,1)-1)');
     for j = 1:gen_block(1)
       if i ~= j
         ind = [];
         ind = findstr(st, ['|' deblank(setstr(name_st(j,:))) '||']);
         if ~isempty(ind)
           top_blk(j) = 1;
           if isempty(find(sub_blk(i,:)==j))
             k = find(sub_blk(i,:) == 0);
             sub_blk(i,k(1)) = j;
           end; % if isempty
         end; % if ~isempty
       end; %if i~=j
     end; %for j = 1:gen
   end; %for i=1:gen
   fprintf('.\n');
   %list all of sub-superblocks
   ind=find(top_blk == 0);
   if isempty(top_blk)
     disp('The superblocks have called recursively, the list of all superblock follows')
     for i=1:gen_block(1)

⌨️ 快捷键说明

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