Can class/object parameters store structs? If not, is there an equivalent?

5 ビュー (過去 30 日間)
Sean
Sean 2011 年 9 月 28 日
回答済み: Gaganjyoti Baishya 2020 年 6 月 20 日
I am trying to usefully name internal state variables in a class I am writing (disclaimer: I started doing objected oriented coding yesterday). I have successfully stored scalars and strings in the class/object properties, but I was trying to store a group of memory locations like:
MemLoc.GainArray = [];
MemLoc.OffsetArray = [];
MemLoc.ConvolverMatrix = [];
MemLoc.ConvolverGain = [];
MemLoc.IntegrationTime = [];
Unfortunately, having the period in the name (as happens with a struct) causes an error in matlab. How can I store a struct such as this in an object parameter?

採用された回答

Daniel Shub
Daniel Shub 2011 年 9 月 29 日
The answer is yes you can. Where are you trying to do this? It looks like maybe in the properties section of your classdef file. You need to use the struct function to initialize it, or initialize it in the constructor method.
  1 件のコメント
Sean
Sean 2011 年 9 月 30 日
Thank you for answering. I was indeed trying to set the struct values in the properties section using the same syntax that I normally would at the command line. I didn't even realize there was a struct() command until you mentioned it.

サインインしてコメントする。

その他の回答 (1 件)

Gaganjyoti Baishya
Gaganjyoti Baishya 2020 年 6 月 20 日
Yes definitely you can. I was also wanting the same to be done. So you just need to declare that struct variable inside the properties section and initialise it's value in the constructor of the class.
Something like this:
properties
myStruct;
end
methods
function obj = myClass(obj)
obj.myStruct.myField = 1;
end
end

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by