How to assign a structure name using an index?
Show older comments
Hello, The problem I am having is that when I run the final line of code I receive an error message "Function 'subsindex' is not defined for values of class 'cell'." Abb is a cell array with abbreviations. I am trying to assign the structure name as the abbreviation related the filename I am interested in. Any help would be greatly appreciated.
function [GSF,DateTime,WaterGallon,WUI]=struct_construction(filename)
%%Import date and demand as a function of file name
%Using function to bring in date and demand for specific building
[DateTime,WaterGallon] = date_and_demand(filename);
%%Import building specific attributes (gross square feet, filename,
%%abbreviation, etc.) for all buildings
%Importing attributes from master sheet
[BN,Abb,FN,BuildingType,Year,GSF,Water] = import_master_sheet('Master_sheet.xlsx','Sheet1',2,239);
%%Constructing struct for each building as a function of filename
% Making Struct for Buliding and Computing Water Use Intensity
index = find(strcmp(FN, filename));
B_GSF= GSF(index);
WUI=WaterGallon./B_GSF;
WUI_avg=mean(WaterGallon);
char(Abb(index))=struct('GSF',B_GSF,'DateTime',DateTime,'WaterGallon',WaterGallon,'WUI',WUI,'WUI_avg',WUI_avg);
end
Accepted Answer
More Answers (1)
Honglei Chen
on 12 Jul 2017
Here is a simple example how you can achieve that
Abb = {'file1','file2'};
temp = struct('a',3,'b',4);
index = 1;
eval([Abb{index} '=temp';])
However, using eval is in general discouraged. Have you thought about using a container.Map for this purpose? Have you thought about saving an extra field in the struct array so you can find the correct item by searching the struct array? something like
Abb = struct('name',{'file1','file2'},'a',{3 4},'b',{5 6})
Abb(cellfun(@(x)strcmp(x,'file1'),{Abb.name}))
HTH
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!