Storing output of python call to class

조회 수: 8 (최근 30일)
Trey Shenk
Trey Shenk 2023년 11월 7일
답변: Neha 2023년 11월 20일
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
properties
sigmf
end
methods
function obj = fromfile(obj, filename)
% Create a python sigmffile and store for processing
obj.sigmf = py.sigmf.sigmffile.fromfile(filename);
end
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)));
end
end
end
I'm calling it using
x = SigMF()
x.fromfile("test")
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.

답변 (1개)

Neha
Neha 2023년 11월 20일
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:
x=x.fromfile("test");
samples=x.read_samples(1, 100);
Another way to avoid this issue would be to inherit the "handle" class:
classdef SigMF < handle
...
end
For more information about the difference between value and handle classes, you can refer to the following documentation link:
Hope this helps!

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by