Vector output prints one after another

1 次查看(过去 30 天)
I'm trying to print the results of four equations from a four loop in five columns including time. When I do it it prints the data in five columns but the data is one after another. I want each data to be side by side going down. Does anyone have any suggestions?
clc;
close all;
clear all;
theta0 = pi/3; g = 9.81; L = 1;
ti = 0; tf = 20; dt = 0.005;
nt = (tf-ti)/dt;
t = linspace(ti,tf,nt);
theta = zeros(1,nt);
theta(1) = theta0;
omega = zeros(1,nt);
alpha = zeros(1,nt);
Etotal = zeros(1,nt);
H = L - L*cos(theta);
f = 'Time'; z = 'Omega'; h = 'Theta'; p = 'Alpha'; q = 'Energy';
fprintf('Time \t Omega \t Theta \t Alpha \t Energy\n',f, z, h, p, q);
for k = 1:nt-1
omega(k+1) = (-g/L)*sin(theta(k))*dt + omega(k);
theta(k+1) = omega(k)*dt + theta(k);
alpha(k+1) = (omega(k+1) - omega(k))/dt;
Etotal = g*H + (1/2)*(L*omega).^2;
end
fprintf('%5.2f \t %5.2f \t %5.2f \t %5.2f \t %5.2f\n',[t,omega(k),theta(k),alpha(k),Etotal]');

采纳的回答

Star Strider
Star Strider 2020-7-13
If you have R2013b or later, put them in a table:
OutTable = table(t.', omega.',theta.',alpha.',Etotal.', 'VariableNames',{f, z, h, p, q})
Then, comment-out (or delete) the fprintf calls.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by