Creating a data structure by a loop.

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

Risposte (1)

Azzi Abdelmalek
Azzi Abdelmalek il 17 Lug 2016
Modificato: Azzi Abdelmalek il 17 Lug 2016
Use {}
name_of_data_structure(itt).(list_nam(iter))=data_matrix{itt}(:,iter}

2 Commenti

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!
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

Accedi per commentare.

Categorie

Richiesto:

Ben
il 17 Lug 2016

Commentato:

Ben
il 17 Lug 2016

Community Treasure Hunt

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

Start Hunting!

Translated by