help with dynamic variable names set with 'for' incrementer
Ältere Kommentare anzeigen
Hi,
I am trying to split up a large numerical dataset into individual days. What I have that works and what I have that failed is;
function [FxStOut, FxStIndex] = FxSt(FxStIn)
%Struct for Forex
UnqDate = unique(FxStIn(:,1));
UnqL = length(UnqDate);
for ii = 1:UnqL
indx = FxStIn(:,1) == UnqDate(ii);
eval(['dataOut.d' num2str(ii) ' = FxStIn(indx, 2:6);']);
%Have tried but died
%dataOut(ii) = FxStIn(indx, 2:6);
%dataOut.d([ii]) = FxStIn(indx, 2:6);
%dataOut.([ii]) = FxStIn(indx, 2:6);
end
FxStOut = dataOut;
FxStIndex = UnqDate;
Is 'eval' the only way to achieve this, both reading in and out of array. A number/numeral can not be a variable/array name, as I would like;
data.1
data.2
data.3
is this correct.
Can I solve this by using a 3D (:,:,:), however, each depth is a different size (factors different) and I presume that is not allowed.
2 Kommentare
Scragmore
am 27 Nov. 2011
All of the answers say "avoid eval". Read this to know why eval is a really bad way of programming:
Akzeptierte Antwort
Weitere Antworten (2)
bym
am 27 Nov. 2011
0 Stimmen
eval can be evil! avoid if possible.
read this for alternatives: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
Walter Roberson
am 27 Nov. 2011
Replace your line
eval(['dataOut.d' num2str(ii) ' = FxStIn(indx, 2:6);']);
with
dataOut.(['d' num2str(ii)]) = FxStIn(indx, 2:6);
or alternately with
dataOut.(sprintf('d%d',ii)) = FxStIn(indx, 2:6);
1 Kommentar
Scragmore
am 28 Nov. 2011
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!