Syntax other than evil eval to build dynamic dataset?
古いコメントを表示
Have a series of Excel spreadsheets (income/expense for an organization with which I'm involved). Have built a script logic that will find the sections for each sheet with a series of loops
% read income, expense section by fund grouping
in1=1; in2=1;
for incexp={'Income' 'Expenses'}
for grp={'Undesignated' 'Designated' 'Restricted'}
in1=in1+find(~cellfun(@isempty,strfind(t(in1+1:end,1),char(grp))),1);
in2=in2+find(strcmp((t(in2+1:end,3)), ...
['Total ' char(grp) ' ' char(incexp)])); % Section end
% get fund block within group
i1=in1+1;
if strcmp(incexp,'Income')
i2=i1+find(strcmp((t(i1+1:in2,3)), ...
['Total ' char(grp) ' Contributions'])); % Funds block
else
i2=in2;
end
% eliminate variable number of blank lines between last and total
i2=find(~cellfun(@isempty,(t(i1:i2-1,3))),1,'last')+i1-1;
end
% read various values for group, put into dataset here...
....
end
At the point of the last comment and ellipses above it would be convenient to be able to use the variable incexp as the dataset name. As the question title poses, is it possible without the use of eval? As per the usual for it, got into a passle of trouble trying to write the string. Dynamic fields for structures is similar idea which works but afaik there's not an equivalent to pick/set the structure name?
(incexp).(grp).Fund=t(in1:in2,3); % ideal (but illegal) reference
採用された回答
その他の回答 (1 件)
Kelly Kearney
2014 年 11 月 10 日
編集済み: Kelly Kearney
2014 年 11 月 10 日
It's not ideal (since it can confuse the interpreter), but I've occasionally used load/save to accomplish this.
Tmp.(incexp).(grp).Fund=t(in1:in2,3);
save tempfile -struct Tmp;
clear Tmp;
load tempfile;
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!