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

24 次查看(过去 30 天)
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

回答(1 个)

Matt J
Matt J 2025-11-5,18:11
编辑:Matt J about 15 hours 前
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

类别

Help CenterFile Exchange 中查找有关 MATLAB Coder 的更多信息

产品


版本

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by