Cant use Excel names to fprintf?

i´m trying to fprintf strings that display (names, height and weight) of all the persons in an excel document. But since fprintf can´t use 'cell' inputs i cant continue. I´ve tried to change the cells into 'doubles' and 'char' arrays but nothing seems to work?
Can anyone help?
gogn = readtable("Book1.xlsx", "VariableNamingRule","preserve");
gogn.Properties.VariableNames;
[tblB,index] = sortrows(gogn);
fname = tblB.Fornafn; % first name
lname = tblB.Eftirnafn;% last name
weight = tblB.("[pund]");% pounds
height = floor(tblB.("[foot.inch]"));%feets
for k = height
meters = k/3.2808; %chage to meters
fprintf('%s %s is %.2f meters and %.2f pounds./n',fname,lname,meters,weight)
end
Error using fprintf
Function is not defined for 'cell' inputs.

 Accepted Answer

gogn = readtable("Book1.xlsx", "VariableNamingRule","preserve");
[tblB,index] = sortrows(gogn);
fname = tblB.Fornafn; % first name
lname = tblB.Eftirnafn;% last name
weight = tblB.("[pund]");% pounds
height = floor(tblB.("[foot.inch]"));%feets
meters = height/3.2808; %change to meters
for k = 1:numel(meters)
fprintf('%s %s is %.2f meters and %.2f pounds.\n',fname{k},lname{k},meters(k),weight(k))
end
Camila Ferguson is 1.52 meters and 103.91 pounds. Charlotte Morris is 1.52 meters and 105.31 pounds. Daisy Kelley is 1.52 meters and 112.91 pounds. Daryl Chapman is 1.22 meters and 105.23 pounds. Garry Andrews is 1.22 meters and 112.44 pounds. Henry Williams is 1.22 meters and 113.35 pounds. Jared Baker is 1.52 meters and 109.82 pounds. Kirsten Crawford is 1.22 meters and 108.70 pounds. Lucia Allen is 1.52 meters and 106.13 pounds. Rafael Clark is 1.52 meters and 114.77 pounds. Ryan Cameron is 1.52 meters and 106.50 pounds.

1 Comment

thank you so much, that's exactly what i meant.

Sign in to comment.

More Answers (2)

see this example
fname={'abc'};
fprintf('%s',fname)
Error using sprintf
Function is not defined for 'cell' inputs.
fprintf('%s',fname{1})
You can convert your cell array into a string array (assuming it contains text data) using string or into a numeric array (assuming the elements are compatibly sized) using cell2mat.
c = {'Benjamin', 'Steve', 'Cleve'}
c = 1×3 cell array
{'Benjamin'} {'Steve'} {'Cleve'}
s = string(c)
s = 1×3 string array
"Benjamin" "Steve" "Cleve"
fprintf('%s\n', s)
Benjamin Steve Cleve
dc = {1, 2, 3, 4}
dc = 1×4 cell array
{[1]} {[2]} {[3]} {[4]}
d = cell2mat(dc)
d = 1×4
1 2 3 4
fprintf('%d\n', d)
1 2 3 4

Categories

Products

Release

R2022b

Asked:

on 4 Nov 2022

Commented:

on 4 Nov 2022

Community Treasure Hunt

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

Start Hunting!