How to use structures as properties of a matlab class ?
99 ビュー (過去 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
0 件のコメント
採用された回答
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
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
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!