How to create an empty dictionary with a struct as value

I want to create a dictionary with a defined struct as the value. What i currently have is:
dict = dictionary(string.empty, struct(test={}));
The problem is that the empty dictionary throws an error when i try to access the struct it throws an error that the field name does not exist:
dict.values.test
Unrecognized field name "test".
I want that the dictionary behave like when you try to access an empty struct with a field. Is there any way so the dictionary remembers the field values?

7 件のコメント

Jan
Jan 2022 年 12 月 8 日
What do you expect struct(test={}) to do? Why do you think, that this creates a struct called "test"?
Guido Hiller
Guido Hiller 2022 年 12 月 8 日
I expect it to create an empty struct which has the field test.
str = struct(test={});
I can then access the field of the empty struct without it throwing errors and just returning nothing.
str.test
As far as I understand it, when I use the struct in the dictionary it erases the field (?), so dict.values.test returns an error.
Rik
Rik 2022 年 12 月 8 日
@Jan, As you can see below, it actually does something, as this is using the Name=Value syntax. So it is equivalent to the second call:
str = struct(test={})
str = 0×0 empty struct array with fields: test
str = struct('test',{})
str = 0×0 empty struct array with fields: test
Jan
Jan 2022 年 12 月 9 日
編集済み: Jan 2022 年 12 月 9 日
@Rik: Thanks. I ran the same code struct(test={}) yesterday here in the forum and got an error message. Strange.
@Guido Hiller: Thanks for your explanation. What is the meaning of assining an empty string in a dictionary? What about considering as a bug, that the dictionary command acceptes an empty value for an empty key? Would stopping with an error message be better?
Or in other words: Is an empty dictionary meaningful? I know, there are empty structs with fields, so maybe the answer is "yes".
Guido Hiller
Guido Hiller 2022 年 12 月 9 日
@Jan The dictionary is filled based on user input and there is a legit case that the dictionary is just empty. Basically it is just me being to lazy to write an if else clause everytime around the getter.
Another methode i am currently testing is creating a custom class which contains just the properties from the struct, which seems to work better with the dictionary.
Paul
Paul 2022 年 12 月 9 日
I still don't understand what the use case is for having an empty, configured dictionary where the value of the empty dictionary is a struct with a field. What would you do with that empty dictionary, which I guess is the same question asked by @Jan.
If we configure a dictionary with an empty string key and empty struct value
d = dictionary(string.empty,struct.empty)
d =
dictionary (stringstruct) with no entries.
we can subsequently add entries to the dictionary
d("a") = struct(test=1);
d("b") = struct(test=2);
d.values returns a struct array
d.values
ans = 2×1 struct array with fields:
test
which, in this case can be accessed as a struct and the values in the test field turned into an array via a comma separated list in the usual way
[d.values.test]
ans = 1×2
1 2
We can add an entry with a different type of value in the test field
d("c") = struct(test="foo")
d =
dictionary (stringstruct) with 3 entries: "a" ⟼ 1×1 struct "b" ⟼ 1×1 struct "c" ⟼ 1×1 struct
d.values
ans = 3×1 struct array with fields:
test
d.values.test
ans = 1
ans = 2
ans = "foo"
We can even add a struct with a different field
d("d") = struct(other=100)
d =
dictionary (stringstruct) with 4 entries: "a" ⟼ 1×1 struct "b" ⟼ 1×1 struct "c" ⟼ 1×1 struct "d" ⟼ 1×1 struct
But now we can't access values as above because the struct values are different and can't themeselves be concateneated into a struct array
try
d.values
catch ME
disp('error')
end
error
But we can still get the values in a cell array
values(d,"cell")
ans = 4×1 cell array
{1×1 struct} {1×1 struct} {1×1 struct} {1×1 struct}
Guido Hiller
Guido Hiller 2022 年 12 月 9 日
@Paul Thank you for your answer. I think I had a misunderstanding about the behaviour of structs. If you can put structs with different fields into the same dictionary, then my inital question does not make any sense.

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

製品

リリース

R2022b

質問済み:

2022 年 12 月 8 日

コメント済み:

2022 年 12 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by