How to define size of class property depending on a given parameter (including codegen)

29 visualizaciones (últimos 30 días)
Stefan
Stefan el 5 de Nov. de 2025 a las 14:53
Editada: Matt J el 6 de Nov. de 2025 a las 9:06
I want to create a class, that is able to handle different property sizes depending on some configuration parameter, which is constant from beginning of the script or at compile time.
For Example, I would like to change the size (513) of the fft in this Class depending on configuration. The solution should work with matlab coder.
classdef FFT_Object
properties
FFT = complex(zeros([1 513],'single'),zeros([1 513],'single'));
end
function obj = FFT_Object()
end
end

Respuestas (1)

Matt J
Matt J el 5 de Nov. de 2025 a las 18:11
Editada: Matt J el 6 de Nov. de 2025 a las 9:06
With the classdef below, you can set the default size with its defaultSize() static method. This will remain in effect until the script is re-run. E.g.,
FFT_Object.defaultSize(1000);
new_obj=FFT_Object(); %default object of size 1x1000
classdef FFT_Object
properties
FFT
end
methods
function obj = FFT_Object(inputFFT)
if ~nargin
s=FFT_Object.defaultSize;
obj.FFT = complex(zeros([1 s],'single'),zeros([1 s],'single'));
else
obj.FFT=inputFFT;
end
end
end
methods (Static)
function varargout=defaultSize(n)
persistent siz
if isempty(siz), siz=513; end
if nargin, siz=n; end
if nargout, varargout={siz}; end
end
end
end

Categorías

Más información sobre MATLAB Coder en Help Center y File Exchange.

Productos


Versión

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by