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

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

 Respuesta aceptada

Stephen23
Stephen23 el 11 de Feb. de 2016
Editada: Stephen23 el 19 de Jun. de 2019

7 comentarios

Stefano Alberti's comment mistakenly posted as an answer's moved here:
Thank you Stephen, It's very hard for me find a solution for my trouble. But I can try to do that!
I think that I want to do, it's really simple. I want take for each for cycle two values (from a string) and put this in two columns inside a struct or cell or table or 'what do you want', and for each cycle add below the first line the second values ecc ecc. After this, I've a two separate columns with time1 and time2 filled wiht my all values, I'd apply the elapsed time fuction between columns 1 and 2.
You thinks that is it possible ?
Thanks,
Stefano
Stephen23
Stephen23 el 12 de Feb. de 2016
Editada: Stephen23 el 12 de Feb. de 2016
@Stefano Alberti: Your code needs some improvement. For example you fopen and fclose the file, but you never use this file while it is open. Do not make code more complicated than it needs to be!
I notice that in the last two days you have asked many question around this topic, but you never really told us what you are trying to achieve. It is usually more effective to describe what you want to achieve (i.e. you task) and then we can show you an efficient way of doing it. Your task seems to be to read the file names and calculate the elapsed time based on the timestamps in each filename. Here is a efficient way of doing this:
C = {'Daily_cum_2013_05_30_09_59__2013_05_31_10_05.asc';...
'Daily_cum_2015_02_11_08_16__2015_02_11_08_42.asc';...
'Daily_cum_1999_12_31_23_59__2000_01_01_00_01.asc'};
[mtc,spl] = regexp(C,'\d{4}(_\d{2}){4}','match','split');
mtc = vertcat(mtc{:});
%spl = vertcat(spl{:});
dtv = datevec(mtc(:),'yyyy_mm_dd_HH_MM')
out = etime(dtv(1+end/2:end,:),dtv(1:end/2,:))
and its output (the time differences in seconds):
out =
86760
1560
120
Stephen, you're right! I don't want that you compile my code, my difficulty is make a right code that works well, together. Single code is quite simple, in MATLAB there are many ways to reach the gol.
With your beatiful code, I try to create a for cycle but the answer is:
Error using regexp Invalid option for regexp: daily_cum_2013_05_30_11_32__2013_05_31_11_38.asc.
Why ?
Thanks
Stefano
Stephen23
Stephen23 el 12 de Feb. de 2016
Editada: Stephen23 el 12 de Feb. de 2016
Please show the complete code that you are using and the complete error message.
Why do you need a loop? I just showed you a method of processing all of the filenames without any loop. What do you want the loop for?
clear all
D = dir(fullfile('Daily_cum_*.asc'));
mkdir('new')
for k =1:3 %numel(D)
fid = fopen(fullfile(D(k).name), 'rt');
filename = D(k).name ;
A=importdata(filename, ' ', 6);
B=A.data;
Nan_value = sscanf(A.textdata{6,1},'%*s %f');
DC = changem(B, 0, Nan_value);
cd('new');
dlmwrite(filename,DC, ' ');
cd ('..')
fclose(fid);
[mtc,spl] = regexp(D.name,'\d{4}(_\d{2}){4}','match','split');
mtc = vertcat(mtc{:});
%spl = vertcat(spl{:});
dtv = datevec(mtc(:),'yyyy_mm_dd_HH_MM')
out = etime(dtv(1+end/2:end,:),dtv(1:end/2,:))
end
Error using regexp
Invalid option for regexp: daily_cum_2013_05_30_11_32__2013_05_31_11_38.asc.
Stephen23
Stephen23 el 12 de Feb. de 2016
Editada: Stephen23 el 12 de Feb. de 2016
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'
Thank you very much Stephen!

Iniciar sesión para comentar.

Más respuestas (1)

2 comentarios

Thanks Walter, I've tried to read that but I didn't found the solution at my problem.
Walter Roberson
Walter Roberson el 11 de Feb. de 2016
Editada: Stephen23 el 11 de Feb. de 2016
"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.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Identification en Centro de ayuda y File Exchange.

Preguntada:

el 11 de Feb. de 2016

Editada:

el 19 de Jun. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by