For loop for string elements and data on a column
1 view (last 30 days)
Show older comments
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 Comments
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
More Answers (0)
See Also
Categories
Find more on Earth and Planetary Science 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!