How improve for cycle with sequential variables name and time evaluate ?

조회 수: 2 (최근 30일)
I've this for cycle:
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'];
clearvars out1 out2;
fclose(fid);
end
I want to assign a cumulative name at the variables 'out' like 'out-1', 'out-2', ... for each for cycle. I try do that with this code:
t1 = cell(numel(D), 1);
t2 = cell(numel(D), 1);
for j=1:numel(D);
t1{j} = sprintf('TimeStart %u', j);
t2{j} = sprintf('TimeFinish %u',j);
end
But I don't now how put this code in the for cycle.
I need to evaluate che difference between two dates & times, and for do that I use this code:
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');
e=etime(t2, t1)
I need to do that with a iterative cycle, for each cell and create a file with the column of results.
Thanks in advance.
Stefano

채택된 답변

Stephen23
Stephen23 2016년 2월 11일
편집: Stephen23 2019년 6월 19일
  댓글 수: 7
Stephen23
Stephen23 2016년 2월 12일
편집: Stephen23 2016년 2월 12일
Don't use my code inside the loop. Just give it a cell array of the filenames:
C = {D.name};
% my code here
for k = 1:numel(D)
out(k) % the time difference
% your code here
end
and it will work. Putting my code before the loop will be faster and neater, and within the loop you can access the elements of out using indexing:
out(k)
The reason your code does not work is because you misunderstand what
D.name
does. It is explained in the documentation for non-scalar structures. In short this:
D.name
D(1).name, D(2).name, D(3).name, ...,D(end).name
However it does not create a cell array of strings! So when you use it as an input to regexp it is equivalent to lots of separate inputs:
regexp(D(1).name, D(2).name, ...,, D(end).name, ...)
as multiple inputs, exactly as the documentation describes. Those extra inputs are interpreted as optional arguments, but do not match any options and cause an error. If you want to have those strings in a cell array, then put your list into a cell array:
{D.name}
to make a cell array of strings. It is easy to check this behavior yourself:
>> S = struct('name',{'A','B','C'});
>> S.name % comma-separated list = separate variables!!
ans =
A
ans =
B
ans =
C
>> {S.name} % put them in a cell array
ans =
'A' 'B' 'C'

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2016년 2월 11일
  댓글 수: 2
Stefano Alberti
Stefano Alberti 2016년 2월 11일
Thanks Walter, I've tried to read that but I didn't found the solution at my problem.
Walter Roberson
Walter Roberson 2016년 2월 11일
편집: Stephen23 2016년 2월 11일
"I want to assign a cumulative name at the variables 'out' like 'out-1', 'out-2'"
Don't do that. Indices have no place in variable names. If you want to index, use a cell array or struct array.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by