Storing Variables in a loop to out put in a table

3 views (last 30 days)
The problem I am trying to solve is I need to output 7 variables at 9 different times periods, IE i need a b c ... etc at t1 and then tn = t(n-1) + T/7, but i need each variable saved for each time t2, t3, t4 etc and then output these varibles in a table and 2 of them in a plot, what would be the most efficient way to do this?

Answers (2)

Mathieu NOE
Mathieu NOE on 6 Dec 2022
hello
try this
clearvars
dt = 0.1;% t increment
iter = 9; % iterations
for ci = 1:iter
time(ci,1) = 1+(ci-1)*dt;
a(ci,1) = (ci-1);
b(ci,1) = (ci.^2);
c(ci,1) = ci+rand(1,1);
d(ci,1) = rand(1,1);
e(ci,1) = rand(1,1);
f(ci,1) = rand(1,1);
end
% figure
plot(time,c,'-*b')
% Create a table from a numeric matrix
X = [time a b c d e f];
T = array2table(X,'VariableNames',{'time' 'a' 'b' 'c' 'd' 'e' 'f'});

Peter Perkins
Peter Perkins on 9 Dec 2022
Make a timetable:
i = (1:9)';
a = i-1;
b = i.^2;
c = i + rand(9,1);
d = rand(length(i),1);
e = rand(length(i),1);
f = rand(length(i),1);
tt = timetable(a,b,c,d,e,f,RowTimes=seconds(1)+milliseconds(100*(i-1)))
tt = 9×6 timetable
Time a b c d e f _______ _ __ ______ ________ _______ ________ 1 sec 0 1 1.485 0.63547 0.26276 0.41905 1.1 sec 1 4 2.0548 0.70826 0.50395 0.014085 1.2 sec 2 9 3.7906 0.61829 0.25581 0.69445 1.3 sec 3 16 4.6088 0.5932 0.66017 0.61009 1.4 sec 4 25 5.6741 0.77162 0.47562 0.8972 1.5 sec 5 36 6.9092 0.88745 0.19149 0.6939 1.6 sec 6 49 7.8361 0.8369 0.29993 0.72113 1.7 sec 7 64 8.1654 0.060169 0.1494 0.82721 1.8 sec 8 81 9.1434 0.64332 0.85963 0.36138
stackedplot(tt)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!