How to dynamically name variables based on a list and assign arrays from a cell array

Hi, I have two elements I would like to combine:
A 1x5 Cell Array called Input with the variables names I want to use:
Input ={'Type','Characteristics','PortfolioMKV','PortfolioRet','StartDate'}
And a 1x5 Cell Array called AllRanges with the data I want to assign to the variable names above:
AllRanges = 'RB' { 4x54 cell} { 2x39 cell} { 2x39 cell} '7/31/2013'
So, I want to dynamically create/ assign the following:
Type = 'RB' Characteristics = { 4x54 cell} PortfolioMKV = { 2x39 cell} PortfolioRet = { 2x39 cell} StartDate = '7/31/2013'
Any idea how I can achieve this? Thanks

1 commentaire

Matt J
Matt J le 22 Sep 2013
Modifié(e) : Matt J le 22 Sep 2013
Once you've dynamically created these variables, won't you also need a dynamic way of accessing/manipulating them later? How were you planning to do that? Was that going to be a follow-up question?

Connectez-vous pour commenter.

Réponses (2)

4 commentaires

Thanks Azzi, thanks Imagine Analyst. Unfortunately neither suggestions give me what I am after. Have a look at the below (not working code):
varnames = {'ab','bb','cb','db'}; values = {[1 2 3], [3 4 5], [6 6 6], 'something'};
for k = 1:length(varnames) CurrVarname=cell2mat(varnames(k)); CurrValue=cell2mat(values(k)); ??which code do I have to use here?? end
Ultimately, I want to get 4 variables with the following values: ab = [1 2 3] bb = [3 4 5] cb = [6 6 6] db = 'something'
What do I need to insert in the loop in order to get the desired results? Is that possible at all?
Actually, I can get the following to work:
varnames = {'ab','bb','cb','db'};
values = {[1 2 3], [3 4 5], [6 6 6], 'something'};
for k = 1:length(varnames)
CurrVarname=cell2mat(varnames(k));
CurrValue=values(k);
Inputdata.(CurrVarname)=CurrValue;
end
In the above case, the variables are all preceeded by "Inputdata.". Is there a way to omit the "Inputdata." part, such that I only have the pure variable name as stored in varnames?
No, not unless you hard code the names in there or go against the recommendations laid out in the FAQ.
@Sven: Please read the linked FAQ topic again until it gets clear to you. Do not create variables dynamically. This produces more troubles than it solves.

Connectez-vous pour commenter.

Catégories

Produits

Question posée :

le 22 Sep 2013

Community Treasure Hunt

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

Start Hunting!

Translated by