how do I make first name of my struct variable?

Hello all, I'm trying to make the first name of my struct variable but I can't figure out how to do it.
This works
test.a = 1;
List = 'a';
test.(List)
ans = 1
I'm trying to do the following:
test.a = 1;
List = 'test'
(List).a
%now I want to get the output test.a but I get:
(List).a
|
Error: Unexpected MATLAB operator.
Is it possible what im trying to do and if so how?
regards, Nick

2 comentarios

Friedrich
Friedrich el 6 de Ag. de 2013
Why do you need this? This doesn't seem like a good way of programming. Maybe if you explain why you need this, we can help you with a better more suitable way of achieving your goal?
Nick
Nick el 6 de Ag. de 2013
Editada: Nick el 6 de Ag. de 2013
I have several counters that have different numbers I need to test. I can put them in a string easy using somthing like this:
CounterNumber = 2;
List = sprintf('%s%d','Counter',CounterNumber)
%output List: Counter2
this i want to use to test what value's my counters have in an for loop:
for ii = 1:Amount
if List.value(ii) == 1
count = count+1;
if count == Amount
CountingDone = true;
else
CountingDone =false;
end
else
CountingDone = false;
end
end
If I have to make an forloop for every counter I have I would go over 60 loops.

Iniciar sesión para comentar.

 Respuesta aceptada

David Sanchez
David Sanchez el 6 de Ag. de 2013
List = 'test' ;
eval(strcat(List,' .a = 1'));
test =
a: 1

4 comentarios

Nick
Nick el 6 de Ag. de 2013
works perfect thankyou
Jan
Jan el 6 de Ag. de 2013
No. Nick, this is not perfect, but a bad programming method. You find hundreds of warnings about the EVAL command in this forum. It impedes the debugging and slows down Matlab noticeably, because it injects variables dynamically. There are always better solutions.
Thanks for the advice on programming Jan Simon. I changed my structs from several to one struct like this:
before:
counter1.data
counter2.data
...
counterX.data
after:
for jj = 1:X
for ii = 1:n
counter(jj).data(ii)
end
end
With this I don't need the EVAL command anymore.
Regards,
Jan
Jan el 7 de Ag. de 2013
This is a nice and efficient solution! Thanks for posting it, Nick.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Centro de ayuda y File Exchange.

Preguntada:

el 6 de Ag. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by