Skip to main content

MATLAB Interface

PSIM's Python class can be called from MATLAB using MATLAB's Python interface. Here's how to set it up and use it:

Prerequisites

1. Compatible Python version MATLAB requires a specific Python version. Check yours:

pyenv
where python

MATLAB 2023b+ supports Python 3.9–3.11. Make sure your Python installation matches.

2. Tell MATLAB which Python to use (if needed):

pyenv('Version', 'C:\Python311\python.exe')

Basic Setup

% Import the module
psim_module = py.importlib.import_module('psimapipy');

% Create the PSIM object (empty string = auto-detect from registry)
psim = psim_module.PSIM('');

% Verify it loaded correctly
if ~psim.IsValid()
error('PSIM failed to load');
end

Running a Simulation

% Run a simulation
result = psim.PsimSimulate('C:\sims\boost.psimsch', 'C:\sims\boost.smv', ...
pyargs('simview', int32(0), 'R1', '10', 'C1', '1e-6'));

if result.IsError == 0
% Access a curve by name
v_out_curve = result.Graph{'V_out'}; % MATLAB cell-style indexing for Python __getitem__

% Get number of rows
nRows = v_out_curve.Rows;

% Read a specific sample value
val = double(v_out_curve{int32(100)}); % 101st sample (0-indexed)
fprintf('V_out[100] = %.4f\n', val);
else
disp(char(result.ErrorMessage));
end

Note on pyargs: Use pyargs to pass Python keyword arguments from MATLAB. All parameter override values must be strings.


Reading Graph Curves

% 1. After Basic Setup

% 2. Read File
file_path = 'C:\sims\boost.smv';
result = psim.PsimReadGraphFile(file_path);

% Inspect the Graph container
properties(result.Graph)

% If 'curves' is a property, inspect the first curve
first_curve = result.Graph.curves{1};
properties(first_curve)
```

---

## Accessing Graph Curves

```matlab
graph = result.Graph;

% By name
curve = graph{'V_out'};

% By index (0-based in Python)
curve = graph{int32(0)};

% Number of curves
n_curves = graph.__len__();

% Iterate over all curves
curve_list = graph.curves;
for i = 1:n_curves
c = curve_list{int32(i-1)};
fprintf('Curve: %s, Rows: %d\n', char(c.Name), c.Rows);
end

% Convert a full curve to a MATLAB array
n = double(curve.Rows);
values = zeros(1, n);
for i = 1:n
values(i) = double(curve{int32(i-1)});
end
plot(values);

Modifying a Schematic

% Open schematic
sch = psim.PsimFileOpen('C:\sims\filter.psimsch');

% Set element parameter by type + name
psim.PsimSetElmValue2(sch, 'Resistor', 'R1', 'Resistance', '1000');
psim.PsimSetElmValue2(sch, 'Capacitor', 'C1', 'Capacitance', '10e-6');

% Save modified schematic
psim.PsimFileSave(sch, 'C:\sims\filter_modified.psimsch');

% Simulate the open handle directly
result = psim.PsimSimulate(sch, 'C:\sims\filter_out.smv', pyargs('simview', int32(0)));

Parameter Sweep Loop

py.sys.path().insert(int32(0), 'C:\path\to\Psim_Class');
psim_module = py.importlib.import_module('Psim_Class');
psim = psim_module.PSIM('');

r_values = [10, 20, 50, 100];
vout_finals = zeros(1, numel(r_values));

for i = 1:numel(r_values)
graph_path = sprintf('C:\\sims\\out_R%d.smv', r_values(i));
result = psim.PsimSimulate('C:\sims\circuit.psimsch', graph_path, ...
pyargs('simview', int32(0), 'R_load', num2str(r_values(i))));

if result.IsError == 0
v = result.Graph{'V_out'};
last_idx = int32(double(v.Rows) - 1);
vout_finals(i) = double(v{last_idx});
else
fprintf('Failed for R=%d: %s\n', r_values(i), char(result.ErrorMessage));
end
end

plot(r_values, vout_finals, '-o');
xlabel('R load (\Omega)'); ylabel('V_{out} final (V)');

Running a Script

script_text = 'omega = 2 * 3.14159 * f; Xc = 1/(omega*C); Vout = Vin * Xc / sqrt(R*R + Xc*Xc);';

% Build Python dict for inputs
inputs = py.dict(pyargs('f','1000', 'C','10e-6', 'R','100', 'Vin','12'));

result_dict = psim.Psim_RunScript(script_text, inputs);
dictOut = result_dict{1}; % first return value is the output dict
err_msg = result_dict{2}; % second is the error string

if dictOut.isKey('Vout') % note: use Python dict access
fprintf('Vout = %s\n', char(dictOut{'Vout'}));
end

Key MATLAB ↔ Python Gotchas

IssueSolution
Keyword argumentsAlways wrap with pyargs('key', value, ...)
Integer argumentsCast with int32(n) — MATLAB defaults to double which Python may reject
Python __getitem__ (indexing)Use obj{key} syntax in MATLAB
Python strings returnedWrap with char(...) to convert to MATLAB string
Python floats/ints returnedWrap with double(...) or int32(...)
Multiple return valuesPython tuples come back as a cell array — index with {1}, {2}, etc.
0-based indexingPython is 0-indexed; subtract 1 when converting from MATLAB loop counters
Path separatorsUse \\ or / in strings passed to Python functions

Checking the PSIM Version from MATLAB

ver = psim.get_psim_version();
ver_cell = cell(ver);
fprintf('PSIM version: %d.%d.%d.%d\n', ...
int32(ver_cell{1}), int32(ver_cell{2}), int32(ver_cell{3}), int32(ver_cell{4}));

% Or as a readable string (PSIM 2026+)
disp(char(psim.get_psim_version_name()));

The main things to remember are: use pyargs for keyword arguments, int32() for integer inputs, char() for string outputs, double() for numeric outputs, and {n} (cell indexing) for Python subscript access and tuple unpacking.