Reputation: 349
I have written a small model in Matlab. This model analyses several supply nodes to meet the required amount of demand, in a demand node. Supply nodes are specified in a vector, in which for each timestep the available supply is given.
To meet the demand, supply nodes are analysed subsequently whether they can meet the demand, and the fluxes from the supply nodes to the demand node are updated accordingly. This analysis now uses a fixed order, which is defined by the script code. In pseudocode:
for timestep=1:end
if demand(timestep) > supply_1(timestep)
supply_1_demand(timestep) = supply_1(timestep)
else
supply_1_demand(timestep) = demand(timestep)
end
if remaining_demand(timestep) > supply_2(timestep)
supply_2_demand(timestep) = supply_2(timestep)
else
supply_2_demand(timestep) = demand(timestep)
end
# etcetera, etcetera
end
However, this order in which the supply nodes are analysed must be varied. I would like to read this order from a table, where the order of analysis is given by the order in which they are presented in the table. Thus, the table can look like this
1 supply_4
2 supply_1
3 supply_5
# etcetera
Is there a way of reading variable names from such a table? Preferably, this would be without using eval, as this is very slow (as I've heard), and the model will be extended to quite a lot of nodes and fluxes.
Upvotes: 0
Views: 1469
Reputation: 16035
Maybe you can use structures:
varNames={'supp_1','supp_2','supp_3'};
supply.(varNames{1}) = 3; %%% set a variable by name
display(supply.(varNames{1})) %%% get value by name
ans =
3
Upvotes: 1