Azzera filtri
Azzera filtri

Storing output of python call to class

6 visualizzazioni (ultimi 30 giorni)
Trey Shenk
Trey Shenk il 7 Nov 2023
Risposto: Neha il 20 Nov 2023
I'm trying to make a class that will wrap Python's sigmf module. My first step is to load the sigmf file using a python call and store it for later, but I'm not sure how to store it.
My current class is
classdef SigMF
% Wrapper class for python sigmf module
function obj = fromfile(obj, filename)
% Create a python sigmffile and store for processing
obj.sigmf = py.sigmf.sigmffile.fromfile(filename);
function samples = read_samples(obj, start_idx, count)
% Read a number of samples from a given start index
samples = obj.sigmf.read_samples(pyargs("start_index", int64(start_idx-1), "count", int64(count)));
I'm calling it using
x = SigMF()
This doesn't fail, but it doesn't store the output, so the call to read_samples doesn't work. I can run it step by step in the command line or a script as
x = py.sigmf.sigmffile.fromfile("test")
x.read_samples(pyargs("start_index", int32(0), "count", int32(100)))
I could make read_samples load the file and read samples, loading the file can be slow for large file sizes, so I'd like to just load it once and access it anytime I need it.

Risposte (1)

Neha il 20 Nov 2023
Hi Trey,
I understand that you want to create a class that will wrap the "sigmf" module from Python. By default, MATLAB classes are value classes. Therefore, any modifications made to a property in a method do not persist unless the function actually returns the modified object as an output argument.
To resolve this, you can reassign the object with the output of the "fromfile" function:
samples=x.read_samples(1, 100);
Another way to avoid this issue would be to inherit the "handle" class:
classdef SigMF < handle
For more information about the difference between value and handle classes, you can refer to the following documentation link:
Hope this helps!


Scopri di più su Cell Arrays in Help Center e File Exchange





Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by