필터 지우기
필터 지우기

Create Structure Fields with and Array of Strings

조회 수: 46 (최근 30일)
John F
John F 2011년 11월 22일
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!

답변 (2개)

Fangjun Jiang
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
John F
John F 2011년 11월 22일
I think we're on to something here! Thanks a lot!
Fangjun Jiang
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
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?
  댓글 수: 2
John F
John F 2011년 11월 22일
I'd prefer testdata.Porsche.heavyCarLoad.horsepower
John F
John F 2011년 11월 22일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by