MATLAB 2023a: How to assign multiple values(keys?) to one key using dictionaries?

43 Ansichten (letzte 30 Tage)
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 Kommentare
Vilém Frynta
Vilém Frynta am 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?
Daniel Morales
Daniel Morales am 11 Apr. 2023
The number of devices will vary. I am writing an app in app designer that will know how many devices the user inputs. Each device will have 2 JVparams; the voltage it was measured at, and the current measured at each said voltage (both are equal sized lists of at least 15-20). Each device will have 4 results for now: "PCE", "Voc", "Jsc", & "FF".

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Vilém Frynta
Vilém Frynta am 11 Apr. 2023
Bearbeitet: Vilém Frynta am 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"
D101_1: [1×1 struct] D102_1: [1×1 struct] D103_1: [1×1 struct]
disp(d.D101_1) % display the device D101_1 under structure "d"
Device: 'D101_1' JVParams: [1×1 struct] Results: [1×1 struct]
disp(d.D101_1.JVParams) % and so on ...
Voltage: [] Current: []
d.D101_1.JVParams.Voltage = 1:10 % save some values into Voltage
d = struct with fields:
D101_1: [1×1 struct] D102_1: [1×1 struct] D103_1: [1×1 struct]
d.D101_1.JVParams.Voltage % display (call) the voltage values
ans = 1×10
1 2 3 4 5 6 7 8 9 10
edit: added some notes to make it easier to understand

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 11 Apr. 2023
Bearbeitet: Walter Roberson am 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}

Kategorien

Mehr zu Get Started with Specialized Power Systems finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by