How to make a table of strings from a table of numerics
2 views (last 30 days)
Show older comments
Cavit Ertugrul Sirt
on 15 May 2022
Commented: Cavit Ertugrul Sirt
on 15 May 2022
I want to apply convert_hour function to everyrow but it does some weird stuff that I don't understand
arr = [1;2;3;4];
T = array2table(arr);
convert_hour(T.arr)
function out = convert_hour(hour)
left = floor(hour/60);
right = floor(mod(hour, 60));
if left < 10; left = sprintf("0%d", left); else; left = sprintf("%d", left); end
if right < 10; right = sprintf("0%d", right); else; right = sprintf("%d", right); end
out = sprintf("%s:%s\n", left, right);
end
--------- the output is this
ans =
"00000000:01020304"
0 Comments
Accepted Answer
Star Strider
on 15 May 2022
arr = [1;2;3;4];
T = array2table(arr);
convert_hour(T.arr)
function out = convert_hour(hour)
left = floor(hour/60);
right = floor(mod(hour, 60));
if left < 10; left = compose("0%d", left); else; left = compose("%d", left); end
if right < 10; right = compose("0%d", right); else; right = compose("%d", right); end
out = compose("%s:%s", left, right);
end
.
4 Comments
Steven Lord
on 15 May 2022
If this isn't homework, I'd just convert it to a duration.
numHours = randperm(8760, 10)
d = hours(numHours)
d.Format = 'hh:mm'
or if you want to subtract off days and just give hours and minutes after midnight:
h = rem(d, hours(24))
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!