How to extract continued values from for cycle

조회 수: 4 (최근 30일)
Stefano Alberti
Stefano Alberti 2016년 2월 12일
댓글: Brattv 2016년 2월 12일
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에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by