How to define size of class property depending on a given parameter (including codegen)
29 views (last 30 days)
Show older comments
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
Answers (1)
Matt J
on 5 Nov 2025 at 18:11
Edited: Matt J
on 6 Nov 2025 at 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
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!