How to use structures as properties of a matlab class ?

100 次查看(过去 30 天)
Dear reader,
I would like to be able to use structures inside classdef and I do now know how to do it. Here is a toy example of what I would like:
classdef dummy1
properties
S.a = 0;
S.b = linspace(1,2,1e1);
S.c = [1 2 3];
P.d = 0;
P.e = 1;
end
methods
%constructor
function obj = dummy1()
obj;
end
end
end
Notice the two structures S and P defined inside this class.
However I get an error when running:
dummy1()
Any help is highly appreciated. Once I get through with this, the next challenge would be to have this class ready for code generation.
Thank you

采纳的回答

Guillaume
Guillaume 2015-6-15
编辑:Guillaume 2015-6-15
The declaration and initialisation of a property in a class must follow the form:
variablename = value;
You can assign a structure as that value, but that structure needs to be fully formed, for example with struct (or cell2struct, or a function that return a struct, etc.):
classdef dummy1
properties
S = struct('a', 0, 'b', linspace(1, 2, 10), 'c', [1 2 3]);
P = struct('d', 0, 'e', 1);
end
end
  8 个评论
Leon Stolp
Leon Stolp 2020-4-1
I am trying to achieve the same thing, but for a dependent property.
properties (Dependent)
Numbers = struct (Fn, Rn)
Coeff = struct (C_B, C_WP, C_P, C_M, C_F)
S_BH
R_F
ff = struct (C_14, L_R, formfactor)
Rapp = struct (APP_factor_eq, R_APP)
Rw = struct (C_3, C_2, C_5, C_15, m_4, lambda, C_16, m_1, R_W)
Rb = struct (P_B, Fn_i, R_B)
Rtr = struct (Fn_T, C_6, R_TR)
Ra = struct (C_4, C_A, R_A)
R_T
end
However, I get a "error" that tells me, that Default values should not be assigned to Dependent properties. It's causing further Problems, as I can't access the structs later on in the methods. Is there a way to make it work?
Ludo Houben
Ludo Houben 2023-7-26
@Guillaume how to change the code for 'S' in case you want to use a simulink.bus definition called 'testbus'
Simulink.Bus: testbus
Simulink.Bus.Element: 'value1'
Simulink.Bus.Element: 'value2'

请先登录,再进行评论。

更多回答(0 个)

产品

Community Treasure Hunt

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

Start Hunting!

Translated by