Code covered by the BSD License  

Highlights from
Circuit Analysis Toolbox

Circuit Analysis Toolbox

by

John McDermid (view profile)

 

The circuit analysis toolbox allows you to perform an AC analysis of a circuit.

[cirDesc,refNode,notes,file]=input_file(varargin)
function [cirDesc,refNode,notes,file]=input_file(varargin)
% Reads a circuit description file, returning a descriptive cell array,
% a reference node, notes, and the name of the file read.
%
% The "cirDesc" cell array contains  
%       component types
%       component designators
%       "from" nodes
%       "to" nodes
%       component values
%       component positive tolerances
%       component negative tolerances
%       control branches (of dependent sources)
%
% The "refNode" cell array contains the name of the node to be used as the
% reference (ground) node
%
% The "notes" cell array contains any textual information the user wishes
% to preserve when the file is re-saved
%
% Example:
%        [cirDesc,refNode,notes,file]=input_file
%        [cirDesc,refNode,notes,file]=input_file('my_file_name.cir')

% Author: John McDermid
% Created: September 23, 2009

switch nargin
    case 0
        [fileName,pathName]=uigetfile('*.cir','Get Circuit Description File');
        file=[pathName fileName];
        fid=fopen(file);
    case 1
        % Open the input file
        file=varargin{1};
        fid=fopen(file);
        if fid<0
            error(['File "' file '" was not found!'])
        end
    otherwise
        error('There can only be zero or one string parameters!')
end
% Read the first column in the file
firstColFormat='%s %*[^\n]';
columnOne=textscan(fid,firstColFormat,'Commentstyle','%');

% Locate the line numbers of the block identifiers in the first column
notesBlockIdentifier=strmatch('**NOTES**',columnOne{:},'exact');
refBlockIdentifier=strmatch('**REFERENCE**',columnOne{:},'exact');
cirBlockIdentifier=strmatch('**CIRCUIT**',columnOne{:},'exact');

% Rewind the pointer,ship the fist blank line and the block identifier and
% read the notes block, placing it in a cell array.
frewind(fid);
skip_line(fid);      % Skip the notes block identifier
linesToRead=refBlockIdentifier-notesBlockIdentifier-1;
notes=textscan(fid,'%s',linesToRead,'delimiter','\n','CommentStyle','%');

% Read the reference node name
skip_line(fid);      % Skip the reference block identifier
linesToRead=cirBlockIdentifier-refBlockIdentifier-1;
refNode=textscan(fid,'%s',linesToRead,'CommentStyle','%');
refNode=char(refNode{1});
if isempty(refNode)
    error('A reference node has not been declared!')
end

% Read the circuit description (Assumes all the lines remaining after the
% circuit description block identifier are circuit description lines)
% Convert the results into a single cell.
skip_line(fid);     % Skip the circuit block identifier
temp=textscan(fid,'%s %s %s %s %n %n %n %s','Commentstyle','%');
if numel(temp{8})~=numel(temp{7})
    error('Control Branch must a dependent branch or "none", not left blank!')
end
cirDesc=cell(numel(temp{1}),numel(temp)); % Initialize for speed
for I=1:numel(temp)
    nElements=numel(temp{:,1});
    for J=1:nElements
        cellElement=temp{1,I}(J);
        if iscell(cellElement)
            cirDesc{J,I}=cellElement{:};
        else
            cirDesc{J,I}=cellElement;
        end
    end
end

% Close the file
fclose(fid);

function skip_line(fid)
% Skip one line and any blank lines encountered
result={''};
while isempty(char(result{:}))
result=textscan(fid,'%s',1,'delimiter','\n','CommentStyle','%');
end

Contact us