필터 지우기
필터 지우기

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

조회 수: 46 (최근 30일)
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
Vilém Frynta
Vilém Frynta 2023년 4월 11일
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 2023년 4월 11일
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".

댓글을 달려면 로그인하십시오.

채택된 답변

Vilém Frynta
Vilém Frynta 2023년 4월 11일
편집: Vilém Frynta 2023년 4월 11일
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
  댓글 수: 1
Daniel Morales
Daniel Morales 2023년 4월 12일
Thank you for the interesting approach. I will continue to work through this and get back to you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 4월 11일
편집: Walter Roberson 2023년 4월 12일
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}

카테고리

Help CenterFile Exchange에서 Get Started with Specialized Power Systems에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by