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 commentaires

What is your input to the function?
Garrett Miller
Garrett Miller le 6 Mar 2018
Modifié(e) : per isakson le 6 Mar 2018
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);
Garrett Miller
Garrett Miller le 6 Mar 2018
Modifié(e) : per isakson le 6 Mar 2018
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 } , ...

Connectez-vous pour commenter.

Réponses (2)

per isakson
per isakson le 6 Mar 2018
Modifié(e) : per isakson le 6 Mar 2018
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
Walter Roberson
Walter Roberson le 7 Mar 2018
Modifié(e) : Walter Roberson le 7 Mar 2018
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

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by