Creating a data structure by a loop.

1 次查看(过去 30 天)
Ben
Ben 2016-7-17
评论: Ben 2016-7-17
Hi everyone, I found some code to make a data structure in matlab, my code is below: So far I have imported a bunch of data from excel as 177x10 matrices (ignore the wavelengths one). So I am trying to create data structures where I can start each structure with the names us_mari., us_urban., us_rural. and us_tropo. and have ten different column matrices within each data structure using field names found in the list_nam. My code below will give one of the structures I want:
wavelengths=xlsread('USstandard_collection.xlsx','sheet1','B14:B190');
usmari_data=xlsread('USstandard_collection.xlsx','sheet1','C14:L190');
usrural_data=xlsread('USstandard_collection.xlsx','sheet1','Q14:Z190');
usurban_data=xlsread('USstandard_collection.xlsx','sheet1','AU14:BD190');
ustropo_data=xlsread('USstandard_collection.xlsx','sheet1','AF14:AO190');
list_nam = {'hwy101_left','beach','field_of_rocks','sears','la_cumbre_golf_course',...
'dirt_patch_right','hwy101_rightside','parking_lot_flower','middle_school_parking_lot',...
'stadium'}
for iter=1:length(list_nam)
us_mari.(list_nam{iter})=usmari_data(:,iter);
end
result:
>> us_mari
us_mari =
hwy101_left: [177x1 double]
beach: [177x1 double]
field_of_rocks: [177x1 double]
sears: [177x1 double]
la_cumbre_golf_course: [177x1 double]
dirt_patch_right: [177x1 double]
hwy101_rightside: [177x1 double]
parking_lot_flower: [177x1 double]
middle_school_parking_lot: [177x1 double]
stadium: [177x1 double]
My current code only works for the mari data and I also want to do it over the other three: urban, rural, and tropo, so my code I'm working on so far to create 4 datasets in a loop is below:
name_of_data_structure = {'us_mari', 'us_rural', 'us_urban', 'us_tropo'}
data_matrix = {usmari_data, usrural_data, usurban_data, ustropo_data}
for itt = 1:length(name_of_data_structure)
for iter=1:length(list_nam)
name_of_data_structure(itt).(list_nam{iter})=data_matrix(itt)(:,iter);
end
end
but I get the following error:
Error: File: spectral_data.m Line: 21 Column: 54
()-indexing must appear last in an index expression.
Thanks for all the help and if you see anything else wrong, please let me know! Ben

回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2016-7-17
编辑:Azzi Abdelmalek 2016-7-17
Use {}
name_of_data_structure(itt).(list_nam(iter))=data_matrix{itt}(:,iter}
  2 个评论
Ben
Ben 2016-7-17
Thank you that solved that issue, I just have one more question, now I get this error,
Field assignment to a non-structure array object.
Error in spectral_data (line 21)
name_of_data_structure(itt).(list_nam{iter})=data_matrix{itt}(:,iter);
Thank you, for what you have done so far, it has helped a lot!
Ben
Ben 2016-7-17
I actually got it to work for the most part using this.
name_of_data_structure = {'us_mari', 'us_rural', 'us_urban', 'us_tropo'}
data_matrix = {usmari_data, usrural_data, usurban_data, ustropo_data}
for itt = 1:length(name_of_data_structure)
name_of_data_structure{itt} = struct()
for iter=1:length(list_nam)
name_of_data_structure{itt}.(list_nam{iter})=data_matrix{itt}(:,iter);
end
end
but for some reason, in order to get anything I need to write name_of_data_structure{1}.beach, when I wanted for example was us_rural.beach
Thanks, Ben

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by