Dynamic Structure Fieldnames using Genvarname() Invalid

조회 수: 5 (최근 30일)
Eric
Eric 2012년 1월 13일
The help for genvarname() specifically says "If you use genvarname to generate a field name for a structure, MATLAB does create a variable for the structure and field in the MATLAB workspace. See Example 3, below."
The following code results in the following error at the first instance of genvarname(): "??? Argument to dynamic structure reference must evaluate to a valid field name."
count = 0;
while(true)
count = count + 1;
datafolder{count} = uigetdir(cd,...
'Select the folder containing first test event:');
if(~datafolder{count})
datafolder(count) = [];
break;
end
testname{count} = inputdlg({'Name of this test event'},'Name Test Event');
if(isempty(testname{count}))
error('No name entered');
end
end
for test = 1:length(testname)
testevents.(genvarname(testname{test})) =...
processDataFolder(datafolder{test});
testevents.(genvarname(testname{test})) =...
analyzeData(testevents.(testname{test}));
plotData(...
testevents.(genvarname(testname{test})),...
testname{test});
plotAverages(...
testevents.(genvarname(testname{test})),...
testname{test});
end
Very confused. 'Test120112' was used as the input.

채택된 답변

per isakson
per isakson 2012년 1월 13일
The variable, testname, is a cell array of cells. Your code assumes it to be a cell array of strings
K>> class( testname{1} )
ans =
cell

추가 답변 (1개)

Eric
Eric 2012년 1월 13일
For those interested, as the answer I accepted didn't explicity give the change, it was a one-line error (attributable to 9 straight hours buried in this project):
testname{count} = inputdlg({'Name of this test event'},'Name Test Event');
becomes
testname(count) = inputdlg({'Name of this test event'},'Name Test Event');
Forcing testname to become a cell array of strings, instead of a cell array of cells.
This is why I avoid cells like the plague and just use structs.

카테고리

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