Create Structure Fields with and Array of Strings
조회 수: 39 (최근 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!