cell row to variable

I have a cell array of 2 columns - I want to create a cell variable whose name is the ith row of column 1 and whose value is the ith row of column 2 which may be either char or numeric, for example A{i} = 'fred', B[i} = 10 giving Var.fred = 10. sounds simple and I can do it with eval, but I understand use of eval is deprecated - any ideas appreciated

1 comentario

pfb
pfb el 13 de Abr. de 2015
I was about to suggest using eval... Why is that deprecated?

Iniciar sesión para comentar.

Respuestas (2)

Adam
Adam el 13 de Abr. de 2015

0 votos

If you mean you want to create a field on a structure with the name of the ith row of column 1 then:
Var.( A{i,1} ) = B{i,1};
should work for your example.
If you really want to create an actual variable I would strong advise against it as it is not good practice.
You can refer to variables very simply in code as e.g. 'A' because you declare them by that exact name somewhere else in the code. If you dynamically create a variable name then you are stuck with dynamically recreating its name every time you want to use it (and all its cohorts you created similarly).
Cell arrays, structs (with dynamic fields) or just normal arrays if it is pure numbers are the best way to achieve this.

Categorías

Productos

Preguntada:

el 13 de Abr. de 2015

Editada:

el 19 de Jun. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by