For loop for string elements and data on a column

2 Ansichten (letzte 30 Tage)
Michael Angeles
Michael Angeles am 9 Jan. 2022
Kommentiert: Michael Angeles am 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

Akzeptierte Antwort

Voss
Voss am 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

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by