How to use structures as properties of a matlab class ?

142 views (last 30 days)
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

Accepted Answer

Guillaume
Guillaume on 15 Jun 2015
Edited: Guillaume on 15 Jun 2015
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 Comments
Leon Stolp
Leon Stolp on 1 Apr 2020
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 on 26 Jul 2023
@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'

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!