Create Structure Fields with and Array of Strings
37 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Fangjun Jiang
on 22 Nov 2011
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 Comments
Fangjun Jiang
on 23 Nov 2011
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
on 22 Nov 2011
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?
See Also
Categories
Find more on Structures in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!