Is it possible to fprintf/sprintf each row of elements in a vector using loop?
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Daven Artajo
el 26 de Nov. de 2021
Editada: Daven Artajo
el 26 de Nov. de 2021
I want to display the planets name and relative gravity such as: Moon gravity = 0.165 Earth gravity
One of the moderators recommended using a loop for this task. So, I'm trying to see if it's possible to write a code more effeciently using loops without having to print/display each planets one by one. Or is it not possible?
I've been playing around trying to find how to do it with no success, this is by far the closest I've gotten.
NOTE: the vectors are half than it is shown in the example
% Say for example
names = ["Earth"; "Venus"; "Mars"];
val = ["1.0000"; "0.8975"; '0.3915'];
planets = [names, val]
% So I want to display the strings of each row together
% I created this loop that seems way too long
% When I used fprinf it ONLY shows the last row of strings without displaying -
% other rows
A = planets;
for B = 0:2
C = A(B + 1);
D = C';
E = append(C, " gravity = ");
F = [E(:,1)];
end
for G = 4:6
H = A(G);
I = append(H, " Earth gravity");
J = [I(:,1)];
end
fprintf('Grav. Acce.: ');
fprintf('%s ', F(:,1));
fprintf('%s \n', J(:,1));
0 comentarios
Respuesta aceptada
Stephen23
el 26 de Nov. de 2021
Editada: Stephen23
el 26 de Nov. de 2021
I would not use a loop:
N = ["Earth"; "Venus"; "Mars"];
V = ["1.0000"; "0.8975"; "0.3915"];
fprintf('%s %s Gravity\n', [V(:),N(:)].');
1 comentario
Daven Artajo
el 26 de Nov. de 2021
Editada: Daven Artajo
el 26 de Nov. de 2021
Más respuestas (0)
Ver también
Categorías
Más información sobre Graphics Performance en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!