Create Structure Fields with and Array of Strings

63 views (last 30 days)
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!

Answers (2)

Fangjun Jiang
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
John F
John F on 22 Nov 2011
I think we're on to something here! Thanks a lot!
Fangjun Jiang
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.

Sign in to comment.


Walter Roberson
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?
  2 Comments
John F
John F on 22 Nov 2011
I'd prefer testdata.Porsche.heavyCarLoad.horsepower
John F
John F on 22 Nov 2011
here's an example of what I DON'T want...
here are my inputs, except now we're talking about ships:
shipNames =
'xCraft'
'JHHS_Axial_2734'
'JHHS_Axial_2460'
'LCS_1'
'LCS_2_inboard'
'LCS_2_outboard'
'LCS_3_3251'
'LCS_5_X'
dimType =
'D3' 'CL' 'Length_inboard' 'Length_outboard' 'Lambda'
shipData=struct('ShipName',{shipNames},'DimType',{dimType})
shipData =
ShipName: {8x1 cell}
DimType: {'D3' 'CL' 'Length_inboard' 'Length_outboard' 'Lambda'}
What I want to be able eventually do is say...
shipData.xCraft.D3 = data for xCraft at D3
Any suggestions?

Sign in to comment.

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!