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
回答 (1 件)
Azzi Abdelmalek
2016 年 7 月 17 日
編集済み: Azzi Abdelmalek
2016 年 7 月 17 日
Use {}
name_of_data_structure(itt).(list_nam(iter))=data_matrix{itt}(:,iter}
カテゴリ
ヘルプ センター および File Exchange で Data Type Identification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!