MATLAB 2023a: How to assign multiple values(keys?) to one key using dictionaries?
42 views (last 30 days)
Show older comments
Daniel Morales
on 11 Apr 2023
Edited: Walter Roberson
on 12 Apr 2023
I have a dataset consisting of "devices" (samples) that I have tested. I'd like to create a large dictionary that will create keys based on all of the sample's names, and then subsequently create more keys inside of that sample name corresponding to the "JV Parameters" and "Device Results". Inside of the "JVParameters" would be the voltage and current that was measured (these are both lists), and inside "Results" I'd get the device PCE, Jsc, Voc, and FF.
How can I assign multiple keys to this one sample to hold all of this information? Say if I want to see the performance of a device I can search up dict({D101_1}{Results}{PCE}), or if i want the voltages it was tested at, I could use d({D101_1}{JVParams}{Voltage}).
In Python I could call these keys and assign multiple keys in a dictionary by using [D101_1][Results][PCE] but I'm not sure how to do this in matlab 2023a. Please let me know if this makes any sense or not. Image attached.
2 Comments
Vilém Frynta
on 11 Apr 2023
I would love to help, but first I would need to understand something more before I evaluate the best options.
How many devices do you have?
How many JVParams does each device have? Just one? And does each device have just one Results?
Accepted Answer
Vilém Frynta
on 11 Apr 2023
Edited: Vilém Frynta
on 11 Apr 2023
I think you could approach this via structures, which make sense to me. There's also something called map containers, however I am not very experienced with them. But I think structures could work just fine. Let me show you an example:
% Names of the devices
devsNames = {'D101_1', 'D102_1', 'D103_1'};
% Loop through each device and create an empty nested structure
for i = 1:length(devsNames)
% Define empty JV Parameters and Device Results for this device
JVParams = struct('Voltage', [], 'Current', []);
Results = struct('PCE', [], 'Jsc', [], 'Voc', [], 'FF', []);
% Create a new structure for this device and assign the nested fields
d.(devsNames{i}).Device = devsNames{i};
d.(devsNames{i}).JVParams = JVParams;
d.(devsNames{i}).Results = Results;
end
Now you have a structure "d" containing all your devices (they're also a structure), and each device contains JVParams (also structure) and Results (also a structure). It's basically structures containing structures, which contain structures that contains data.
Be aware that this is just an example with empty structures, which are to be filled be you.
Also, I stored all the devices in structure "d" because it's a good idea to avoid dynamic variables in MATLAB. This also allows you to loop through all devices if necessary.
Let's see the results:
disp(d) % display the structure "d"
disp(d.D101_1) % display the device D101_1 under structure "d"
disp(d.D101_1.JVParams) % and so on ...
d.D101_1.JVParams.Voltage = 1:10 % save some values into Voltage
d.D101_1.JVParams.Voltage % display (call) the voltage values
edit: added some notes to make it easier to understand
More Answers (1)
Walter Roberson
on 11 Apr 2023
Edited: Walter Roberson
on 12 Apr 2023
You can create a dictionary which takes scalar keys, for which the associated values are (distinct) dictionaries.
You can create a dictionary which takes scalar keys, and the associated values are cell arrays. But you have to be a bit careful with that:
d = dictionary;
d("parts") = {{'transmission', 'gearshaft'}}
Notice I had to enclose the cell array inside a different cell array. That is because assignment into a dictionary permits multiple assigment, NAME(VECTOR_OF_KEYS) = CELL_OF_VALUES and key VECTOR_OF_KEYS(K) is assigned value CELL_OF_VALUES{K}
See Also
Categories
Find more on Get Started with Statistics and Machine Learning Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!