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 comentarios

Birdman
Birdman el 6 de Mzo. de 2018
What is your input to the function?
Garrett Miller
Garrett Miller el 6 de Mzo. de 2018
Editada: per isakson el 6 de Mzo. de 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 el 6 de Mzo. de 2018
Editada: per isakson el 6 de Mzo. de 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 } , ...

Iniciar sesión para comentar.

Respuestas (2)

per isakson
per isakson el 6 de Mzo. de 2018
Editada: per isakson el 6 de Mzo. de 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 el 7 de Mzo. de 2018
Editada: Walter Roberson el 7 de Mzo. de 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

Categorías

Preguntada:

el 6 de Mzo. de 2018

Editada:

el 7 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by