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!