- Some structures are "preallocated" using repmat(struct(...)), but when you read the documentation for the function datevec you will learn that it does not output a structure but in fact a numeric array. So this preallocation is useless because it creates an array of the wrong type.
- On every iteration you completely reassign the variables date, dateStart and dateEnd, so on every loop iteration they get completely redefined. This happens because you do not use any indexing to allocate the output into a new element on each iteration. This is also why the incorrect "preallocation" does not cause any error.
- Using the variable name date is a bad idea, as this shadows the inbuilt date function. You can check variable names using which.
- the continue operations are not required, they do absolutely nothing useful in this code.
Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Trasform an array of structs with string in an array of structs with numbers
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi, I have a array of struct: for every struct I want to trasform sting into number with the function datevec and at the end I want to obtain an array of structs with numbers. My code run but at the end I obtain only the value of the last struct and not of all structs. The code is
% Pre-allocated structures
date=repmat(struct('x',0), 1, size(s,2));
dateStart=repmat(struct('Start',0), 1, size(s,2));
dateEnd=repmat(struct('End',0), 1, size(s,2));
%
for i=1:size(s,2)
% gestisce la presenza di struct vuote
if(isempty(s(1,i).locs))
continue
%gestisce l'assenza dei valori di inizio/fine rilevazione
elseif(isempty(s(1,i).places))
continue
else
date=datevec(s(1,i).locs(:,1))
% Data di inizio rilevazione
dateStart=datevec(s(1,i).places.startdate(:,1));
% Data fine rilevazione
dateEnd=datevec(s(1,i).places.endate(:,1));
end
end
Can you help me to find the error?
0 Kommentare
Antworten (1)
Stephen23
am 28 Okt. 2015
Bearbeitet: Stephen23
am 28 Okt. 2015
That code is a bit buggy, and it is not doing what you think it is:
A simpler alternative is to use MATLAB more efficiently:
>> S(3).locs = '2015-03-31';
>> S(2).locs = '2015-02-28';
>> S(1).locs = '2015-01-01';
>> datevec(vertcat({S.locs}))
ans =
2015 1 1 0 0 0
2015 2 28 0 0 0
2015 3 31 0 0 0
OR if the date strings have different lengths or might be missing:
>> X = cellfun(@datevec,{S.locs}','UniformOutput',false);
>> cell2mat(X)
ans =
2015 1 1 0 0 0
2015 2 28 0 0 0
2015 3 31 0 0 0
6 Kommentare
Diese Frage ist geschlossen.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!