help with dynamic variable names set with 'for' incrementer

2 ビュー (過去 30 日間)
Scragmore
Scragmore 2011 年 11 月 27 日
編集済み: Stephen23 2023 年 1 月 19 日
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 件のコメント
Scragmore
Scragmore 2011 年 11 月 27 日
Additional information on how I want to expand this.
for ii = 1:length(x)
opMax = zeros(1:length(x), 1:5);
eval(['opMax(' num2str(ii) ') = max(x.d' num2str(ii) ');']);
end
Each 'x.d1' is a (1:n,1:5) array. I am trying to find the max in each column of 'x.d1' and return the five ans to the corresponding row of new array. This 'eval' and 'num2str' seems really messy and complicated, as I build up complexity I feel this will become less and less readable as code. The above test example does not work, incorrect array size allocation.
Stephen23
Stephen23 2016 年 1 月 12 日
編集済み: Stephen23 2023 年 1 月 19 日
All of the answers say "avoid eval". Read this to know why eval is a really bad way of programming:

サインインしてコメントする。

採用された回答

Teja Muppirala
Teja Muppirala 2011 年 11 月 27 日
You are right in that using EVAL to dynamically generate variable names is generally a messy approach that's not very readable, slow, and doesn't scale well.
One alternative is using cell arrays.
Cell arrays let you store differently sized data, for example:
M{1} = [1 2 3]
M{2} = [1 2; 3 4]
M{3} = 'hello'
M{1} + 10
M{2} * 5
disp(M{3})
  1 件のコメント
David Holdaway
David Holdaway 2012 年 3 月 7 日
Cell arrays appear a great way of doing this! Thanks very much!

サインインしてコメントする。

その他の回答 (2 件)

bym
bym 2011 年 11 月 27 日
eval can be evil! avoid if possible.

Walter Roberson
Walter Roberson 2011 年 11 月 27 日
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 件のコメント
Scragmore
Scragmore 2011 年 11 月 28 日
Walter,
Thanks. Shame you cant accept two ans. Your response answered my question directly but Teja's ans gave me an alternative view and solution to my problem that I am going to run with.
Thanks All

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by