How to create a data structure

2 vues (au cours des 30 derniers jours)
anush
anush le 28 Août 2017
Hallo Everyone, I needed a help in creating a data structure. I am new to this platform. it would be helpful if someones guides me through giving information or an example. I have a set of data's in an excel sheet. I need to create a data structure of it. I have set of turbines(1-5) each are having its own respective towers(1-10) having some field names. If i need to add a field name in any of my tower it should update the turbine too. How can i create a data structure for it. Thank you. Its in the form of tree.
  2 commentaires
KSSV
KSSV le 28 Août 2017
Show us an example......this information is vague....
anush
anush le 28 Août 2017
Modifié(e) : anush le 28 Août 2017
<<
/matlabcentral/answers/uploaded_files/86535/turbinr.PNG>>
i tried
function [ struct_data ] = Initialize_Data_Structure( no_turbines, no_towers ) % Initialize_Data_Structure initialises the data structure and fills with dummy data
struct_data.Diagram.Frequency_Linits=[0,1]; struct_data.Diagram.Windspeed_Linits=[0,1]; struct_data.Diagram.Rotor_Speed_Linits=[0,1]; struct_data.Diagram.Gen_Speed_Linits=[0,1];
for ii=1:no_turbines struct_data.Turbine(ii,1).Name=''; struct_data.Turbine(ii,1).Gear_Ratio=0; struct_data.Turbine(ii,1).Rotor.Speed=zeros(2,1); struct_data.Turbine(ii,1).Rotor.Mode_Names=cell(2,1)'; struct_data.Turbine(ii,1).EF_Names={'EF1','EF2'}; struct_data.Turbine(ii,1).EF_Wind_Speed=zeros(2,1); struct_data.Turbine(ii,1).EF_Rotor_Speed=zeros(2,1);
for jj=1:no_towers
struct_data.Turbine(ii,1).Tower(jj,1).Name= num2str(jj,'Tower %d');
struct_data.Turbine(ii,1).Tower(jj,1).EF_Data= zeros(2,2);
% first dimension Names, second dimension operating points (wind, speed)
end
end
end
Each turbine has respective all towers.

Connectez-vous pour commenter.

Réponses (1)

Karan Singh
Karan Singh le 4 Fév 2025
Hi @anush,
I'm not entirely sure I understood your question correctly, but from what I gathered, you need a data structure (DS) that contains 5 generators, each with 10 towers.
I think using a "struct" in MATLAB would be a good option for this. https://in.mathworks.com/help/matlab/ref/struct.html
turbines = struct();
% Loop through turbines (1-5) and towers (1-10)
for turbineID = 1:5
for towerID = 1:10
% Create a tower structure with initial fields
turbines(turbineID).Tower(towerID) = struct( ...
'TowerID', towerID, ...
'PowerOutput', rand()*100, ... % Example field
'Status', 'Active' ... % Example field
);
end
end
disp(turbines);
1x5 struct array with fields: Tower
  • Now to add some new field to a specific tower
turbines(3).Tower(5).WindSpeed = 12.5; % Add 'WindSpeed' to Tower 5 of Turbine 3
  • To update all towers with new field.
for t = 1:length(turbines)
for tw = 1:length(turbines(t).Tower)
turbines(t).Tower(tw).Temperature = 25; % Add 'Temperature' field
end
end
  • Retrive a data from a specific tower
towerData = turbines(2).Tower(7);
disp(towerData); % Display all fields of Tower 7 in Turbine 2
TowerID: 7 PowerOutput: 47.5846 Status: 'Active' Temperature: 25
Before you proceed, as I already mentioned, I don't know much about your specific requirements. So, I haven't considered the time complexity of each operation. These operations can likely be optimized further, but for now, I have used a brute-force approach.
Karan

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by