Created
August 16, 2016 23:29
-
-
Save ustun/18bba137afb28123a6f23cc01e03cf0f to your computer and use it in GitHub Desktop.
Parses (imports) 5Spice data to MATLAB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parse5spice(filename) | |
% PARSE5SPICE Parses the data in the Report tab of the 5Spice software and | |
% imports it into MATLAB workspace | |
% Call the function with an optional filename. If no filename is specified, | |
% a data.txt in the same directory is used. | |
% To create data.txt, right click in 5Spice in the Report tab, click Select | |
% All, and Copy and paste into an empty file. Then, save data as data.txt | |
% in the same directory | |
% The data is automatically exported to workspace, no function output | |
% assignment is needed. | |
% Author: Ustun Ozgur | |
% Date: March 30, 2009 | |
% email: [email protected] for bug reports | |
% TODO: | |
if ~exist('filename') | |
filename = 'data.txt'; | |
end | |
fid = fopen(filename); | |
values = 0; | |
variables = 0; | |
var_names = {}; | |
while 1 | |
tline = fgetl(fid); | |
if ~ischar(tline), break, end | |
if ~isempty(strfind(tline,'Variables:')) | |
variables = variables + 1; | |
continue | |
end | |
if ~isempty(strfind(tline,'Values:')) | |
variables = 0; | |
values = 1; | |
continue | |
end | |
if variables == 2 | |
[tmp1,tmp2,tmp3,tmp4,vals, tmp5] = regexp(tline,'(\s+)\d(\s+)([\w()@#?]+)(\s+)(\w*)(\s)(\w+)'); | |
if isempty(vals) | |
var_names{1} = 'time'; | |
var_types{1} = 'time'; | |
else | |
varname_this = vals{1}(end); | |
var_types{end+1} = vals{1}{end-2}; | |
var_names{end+1} = varname_this{:}; | |
end | |
end | |
if values | |
[tmp1,tmp2,tmp3,tmp4,vals, tmp5] = regexp(tline,'(\d*)(\s+)([\w.-+]+)'); | |
if length(vals) | |
ind = vals{1}(1); | |
if length(ind{:}) > 0 | |
indnew = ind; | |
var_no = 1; | |
else | |
var_no = var_no + 1; | |
end | |
val = vals{1}(3); | |
end | |
evalstr = [var_names{var_no},'(',num2str(str2num(indnew{:})+1), ') = ', val{:},';']; | |
eval(evalstr); | |
end | |
end | |
fclose(fid); | |
%% Plot the data | |
disp('The following vectors have been imported') | |
var_names | |
% some housekeeping | |
clear tmp1 tmp2 tmp3 tmp4 tmp5 ans evalstr fid ind indnew tline val vals values var_no variables varname_this | |
legendstrc = ''; legendstrv = ''; | |
figure('Name','5Spice Import Data') | |
for i = 2:length(var_names) | |
if var_types{i} == 'current' | |
subplot(2,1,2) | |
plot(eval(var_names{1}), eval(var_names{i})) | |
hold all, grid on, axis tight | |
legendstrc = [legendstrc,'''', var_names{i},''',']; | |
xlabel('Time (s)'), ylabel('Current (Amperes)') | |
elseif var_types{i} == 'voltage' | |
subplot(2,1,1) | |
plot(eval(var_names{1}), eval(var_names{i})) | |
hold all, grid on, axis tight | |
legendstrv = [legendstrv,'''', var_names{i},''',']; | |
xlabel('Time (s)'), ylabel('Voltage (Volts)') | |
end | |
end | |
subplot(2,1,2), eval(['legend(', legendstrc(1:end-1),')']); hold off; title('Current(s) vs Time Plot') | |
subplot(2,1,1), eval(['legend(', legendstrv(1:end-1),')']); hold off; title('Voltage(s) vs Time Plot') | |
% Export to calling workspace | |
for i = 1:length(var_names) | |
assignin('base',[var_names{i}], eval(var_names{i})) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment