How to extract continued values from for cycle

1 次查看(过去 30 天)
I've a for cycle that processed thousand files, I don't succeed to take for each for cycle the values generated and store it into a table (or cell or matrix..).
Example:
for cycle return: 1 cycle --> t1 and t2
2 cycle --> t3 and t4
.... n cycle --> tn1 and tn2
creata a table (or something like that to store and give accesible the data) with two separates columns like this:
t1 t2
t3 t4
...
tn1 tn2
Thanks in advance
Stefano

回答(1 个)

Brattv
Brattv 2016-2-12
Hi, I am not sure if t1 and t2 are scalars or vectors/matrices, but is this what you are looking for?
% number of files
n = 1000;
% Making a empty matrix
storeValues = [];
for i=1:n
% Making random numbers as example
t1 = rand(1);
t2 = rand(1);
% Storing values in matrix
storeValues(i,:) = [t1 t2];
end
% Printing vector in command window
storeValues
  3 个评论
Stephen23
Stephen23 2016-2-12
编辑:Stephen23 2016-2-12
Have you read my answer ?
It might resolve a lots of your code problems.
PS: you might also start to learn why it is a bad idea to ask many questions on the same topic: because you cannot keep track of what information people have given you.
Brattv
Brattv 2016-2-12
You need to keep track of your variable types. Use a cell to store the date data that datestr returns. You should also remove the for loop inside the loop.
% Making a empty matrix
storeValues = cell(1,2);
for k =1:3 %numel(D)
fid = fopen(fullfile(D(k).name), 'rt');
filename = D(k).name ;
a=filename(11:end);
b=regexp(a,'__','split');
out1=cellfun(@(x) datestr(datenum(x(1:10),'yyyy_mm_dd'),'yyyy/mm/dd'),b,'un',0);
out2=cellfun(@(x) datestr(datenum(x(12:end),'HH_MM'),'HH:MM'),b,'un',0);
out=[out1' out2'];
prova = [out{1,1}, ' ', out{1,2}];
prova2 = [out{2,1}, ' ', out{2,2}];
format shortg;
t1 = datevec(prova,'yyyy/mm/dd HH:MM');
t2 = datevec(prova2,'yyyy/mm/dd HH:MM');
% Storing values in matrix
storeValues(i,:) = [cellstr(t1), cellstr(t2)];
end
% Printing vector in command window
storeValues

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Elementary Math 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by