# For loop for string elements and data on a column

2 views (last 30 days)
Michael Angeles on 9 Jan 2022
Commented: Michael Angeles on 9 Jan 2022
How can I create a for loop that would create a "Planets" column on the left hand side while the gravity and distance data are also printed out relative to the planet name:
For example:
Planet gravity_data distance_data
xxxx xxxxx xxxx
My code is below: Thanks in advance...
%Free Fall
clear, clc
planet = ["Mercury", "Venus", "Earth", "Moon", "Mars", "Jupiter", "Saturn",...
"Uranus", "Neptune", "Pluto"];
gravity = [3.7, 8.87, 9.8, 1.6, 3.7, 23.12, 8.96, 8.69, 11.0, .58];
time = linspace(0,100,10);
%d = 1/2*(gravity.*time.^2)
[G, T] = meshgrid(gravity, time)
d = 1/2.*(G.*T.^2)
for i = 1:length(planet)
planet(i,:) = [G "_" num2str(i,"%.2f")];
end
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Voss on 9 Jan 2022
%Free Fall
clear, clc
planet = ["Mercury", "Venus", "Earth", "Moon", "Mars", "Jupiter", "Saturn",...
"Uranus", "Neptune", "Pluto"];
gravity = [3.7, 8.87, 9.8, 1.6, 3.7, 23.12, 8.96, 8.69, 11.0, .58];
time = linspace(0,100,10);
%d = 1/2*(gravity.*time.^2)
[G, T] = meshgrid(gravity, time);
d = 1/2.*(G.*T.^2);
for i = 1:numel(planet)
fprintf('%s %.2f\n',planet(i),gravity(i));
end
Mercury 3.70 Venus 8.87 Earth 9.80 Moon 1.60 Mars 3.70 Jupiter 23.12 Saturn 8.96 Uranus 8.69 Neptune 11.00 Pluto 0.58
##### 1 CommentShowHide None
Michael Angeles on 9 Jan 2022
Thank you!

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!