function [data, plot_title] = ssbin2mlab(filename, desired_variable)
% [data, plot_title] = ssbin2mlab(filename, desired_variable) reads a
% Binary Smartspice RAW file 'filename' and extract the variable(s) of
% interest from 'desired_variable' using the file's own variable index
% format and place the data in column into the variable 'data' and also
% return a title into 'plot_title'.
%
% Input argument 'desired_variable' can be omitted if all variables are
% desired. If 'desired_variable' is set to [] (the empty matrix), the
% function will only return the index of all the variables from that file
% and no data is read.
%
% Simon Damphousse, July 09.
if (nargin == 1);
desired_variable = 1;
end
%open the file
fid = fopen(filename,'r');
if fid == -1
error('file not opened');
end
% display the index
if isempty(desired_variable)
go=0;
impr=0;
disp(' ');
disp('The variables are:')
disp(' ');
while (go == 0)
line = fgetl(fid);
w = strfind(line,'Variables:');
if w == 1,
impr=1;
line = line(11:max(size(line)));
end
w = strfind(line,'Binary:');
if w == 1,
impr=0;
go=1;
end
if impr == 1
disp(line);
end
end
data=0;
plot_title = 'noplot';
% Loading data
else
go=0;
while (go == 0)
line = fgetl(fid);
%find the name of the circuit
w = strfind(line,'Input deck file name:');
if ~isempty(w)
id1 = strfind(line,'\');
id1e = id1(max(size(id1)));
id2 = strfind(line,'.cir');
circuit = line(id1e+1:id2-1);
end
%find the plot name.
w = strfind(line,'Plotname:');
if ~isempty(w)
id1 = strfind(line,':');
id2 = max(size(line));
name = line(id1+1:id2);
end
%find the number of variables
w = strfind(line,'No. Variables:');
if ~isempty(w);
id1 = strfind(line,':');
id2 = max(size(line));
nv = str2num(line(id1+1:id2));
end
%find the number of points
w = strfind(line,'No. Points:');
if ~isempty(w);
id1 = strfind(line,':');
id2 = max(size(line));
np = str2num(line(id1+1:id2));
end
go = strcmp(line,'Binary:');
end
if (nargin == 1);
desired_variable = [0:nv-1];
end
disp(['number of variables selected/total: ',num2str(max(size(desired_variable))), '/', num2str(nv)]);
disp(['number of points: ',num2str(np)]);
plot_title = [circuit,': ', name];
% reading all data on the file
donnee = double(fread(fid, nv*np, 'double'));
dim = max(size(donnee));
data = [];
%Sorting data and create matrix
for i=(desired_variable)+1,
data = [data , donnee(i:nv:dim)];
end
end
fclose(fid);