Cell contents reference from a non-cell array object?

function [ information ] = printInfo( subjects )
s=height(struct2table(subjects));
for x=1:s
name=subjects(x).name;
weight=subjects(x).weight;
maxweight=max(weight);
feet=subjects(x).height.feet;
inches=subjects(x).height.inches;
str='%d: %d''%d", %d pounds';
information=sprintf(str,name,feet,inches,maxweight);
end
end

4 个评论

What is your input to the function?
It is a struct array that has a varying amount of subsets, hence the first line of code
s=height(struct2table(subjects));
Which line is the error occurring on?
You should probably be replacing the assignment to s with
s = numel(subjects);
That's kind of the issue, it occurs on the .gui code that I am supposed to be using to check the functionality
clear subjects;
subjects.name = char( randi( 26 , 1 , randi( 10 ) ) + 96 );
subjects.id = randi( 100 );
subjects.weight = randi( 100 , 1 , 2 ) + 100;
subjects.height.feet = randi( 4 ) + 3;
subjects.height.inches = randi( 12 ) - 1;
for s = 2 : randi( 10 )
subjects( s ).name = char( randi( 26 , 1 , randi( 10 ) ) + 96 );
subjects( s ).id = randi( 100 );
subjects( s ).weight = randi( 100 , 1 , 2 ) + 100;
subjects( s ).height.feet = randi( 4 ) + 3;
subjects( s ).height.inches = randi( 12 ) - 1;
end
yourInfo = printInfo( subjects );
for string = 1 : length( yourInfo )
y = 1 - string / length( yourInfo );
uicontrol( 'style' , 'text' , 'units' , 'normalized' , ...
'fontunits' , 'normalized' , ...
'fontname' , 'fixedwidth' , 'string' , ...
yourInfo{ string } , ...
'position' , [ 0 y 1 1 / length( yourInfo ) ] );
end
The error is on yourInfo{ string } , ...

请先登录,再进行评论。

回答(2 个)

yourInfo is a character array, not a cell array, thus replace
yourInfo{ string }
by
yourInfo( string )
After done that correction your code produces a plot and no errors.
btw: string is a poor name of a loop-variable - IMO
function [ information ] = printInfo( subjects )
s=numel(subjects);
for x=1:s
name=subjects(x).name;
weight=subjects(x).weight;
maxweight=max(weight);
feet=subjects(x).height.feet;
inches=subjects(x).height.inches;
str='%s: %d''%d", %d pounds';
information{x}=sprintf(str,name,feet,inches,maxweight);
end
end

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by