Create Structure Fields with and Array of Strings
37 次查看(过去 30 天)
显示 更早的评论
Hello!
Please keep in mind that I'm new to MATLAB.
Structures seem great...but the idea of having to iteratively enter field names for data entry seems ridiculous. Are there any good postings or is there any good advice for creating a strcture in a more automated manner?
Here's what I'm trying to do...
Say I have a "large" data set 3x30 with 30 different fieldNames for the data.
fieldNames={'volts' 'horsepower' 'force' ... 'STDev'} %These are the column headers
Now let's say I have 3 different testConfigurations.
testConfigurations={'noCarLoad';'heavyCarLoad';'lowFuel'} %These are the row headers
Finally, I've done this experiment for 4 different vehicleNames.
vehicleNames = {'Ford' 'Lincoln' 'Chevy' 'Porsche'}
Thus, I have four 3x30 matrices of data.
I want to enter all of this data into a structure called testData and I would like to do it in a way where I don't have to manually enter in all these different fields. All my data is currently in an Excel format with data for each vehicle residing on its own worksheet.
Any suggestions to streamline the structure building process? I really don't feel like having to manually enter 30 field names if I don't have to!!!
Thank you!
0 个评论
回答(2 个)
Fangjun Jiang
2011-11-22
help cell2struct
However, your data is better represented using 3 dimension data.
Names={'volts' 'horsepower' 'force' 'STDev'};
N=length(Names);
Names=cell2struct(mat2cell(1:N,1,ones(N,1)),Names,2)
Vehicle = {'Ford' 'Lincoln' 'Chevy' 'Porsche'};
N=length(Vehicle);
Vehicle=cell2struct(mat2cell(1:N,1,ones(N,1)),Vehicle,2)
Config={'noCarLoad';'heavyCarLoad';'lowFuel'};
N=length(Config);
Config=cell2struct(mat2cell(1:N,1,ones(N,1)),Config,2)
Names =
volts: 1
horsepower: 2
force: 3
STDev: 4
Vehicle =
Ford: 1
Lincoln: 2
Chevy: 3
Porsche: 4
Config =
noCarLoad: 1
heavyCarLoad: 2
lowFuel: 3
If Data=rand(10,10,10), you can reference your data as
Data(Names.volts,Vehicle.Ford,Config.noCarLoad)
2 个评论
Fangjun Jiang
2011-11-23
Set up your three dimension data correctly, you'll be able to use
Data(:,Vehicle.Ford,:) for all the testing data for Ford vehicles, similarly, Data(:,:,Config.noCarLoad) will be testing data for all the "NoCarLaod" configuration.
Walter Roberson
2011-11-22
It can probably be done, but please clarify what you would like the result to look like. For example are you looking for
testdata.Porsche.heavyCarLoad.horsepower
to be a scalar?
Or perhaps
testdata(4).horsepower
should be a vector of 3 values related to Porsche, one value for each test configuration?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!