For loop for string elements and data on a column

1 次查看(过去 30 天)
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

采纳的回答

Voss
Voss 2022-1-9
%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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Earth and Planetary Science 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by