Hi, all
I do have a function running within some loops and create variables with dynamically generated names based on the values of the loops.
The result of my function is a 1000 x 50 matrix of integers. I want to plot the mean of each collumn. Therefore, here's the code
% Distribuição de espécies em grupos
for tipoQSG = tipoQSGmin:tipoQSGmax
for gradienteQSG = gradienteQSGmin:gradienteQSGmax
% Interações interespecíficas
for tipoSIS = tipoSISmin:tipoSISmax
for quantidadeSIS = quantidadeSISmin:quantidadeSISmax
for concentracaoSIS = concentracaoSISmin:concentracaoSISmax
for sinaisSIS = sinaisSISmin:sinaisSISmax
for aleatorizacaoSIS = aleatorizacaoSISmin:aleatorizacaoSISmax
% Efeito de grupos funcionais
for tipoSEG = tipoSEGmin:tipoSEGmax
for sinalSEG = sinalSEGmin:sinalSEGmax
MPDF = myfunc(tipoQSG, gradienteQSG, tipoSIS, quantidadeSIS, concentracaoSIS, sinaisSIS, aleatorizacaoSIS, tipoSEG, sinalSEG)
end end end end end end end end end
I have no problems doing what I need if I type manually, for instance:
plot(mean(MPDF_111111111))
However, when I try to do it in a dynamic way, it fails.
% Distribuição de espécies em grupos
for tipoQSG = tipoQSGmin:tipoQSGmax
for gradienteQSG = gradienteQSGmin:gradienteQSGmax
% Interações interespecíficas
for tipoSIS = tipoSISmin:tipoSISmax
for quantidadeSIS = quantidadeSISmin:quantidadeSISmax
for concentracaoSIS = concentracaoSISmin:concentracaoSISmax
for sinaisSIS = sinaisSISmin:sinaisSISmax
for aleatorizacaoSIS = aleatorizacaoSISmin:aleatorizacaoSISmax
% Efeito de grupos funcionais
for tipoSEG = tipoSEGmin:tipoSEGmax
for sinalSEG = sinalSEGmin:sinalSEGmax
% Checa-se a execução satisfatória do cenário adotado anteriormente. Se
% ele já tiver sido executado em número suficiente de vezes,
% ignora-se-o e passa-se ao próximo.
% Checa se os resultados existem.
% dynamical plot attempt eval(['plot(' 'mean(' 'sprintf(''MPDF_%d%d%d%d%d%d%d%d%d'', tipoQSG, gradienteQSG, tipoSIS, quantidadeSIS, concentracaoSIS, sinaisSIS, aleatorizacaoSIS, tipoSEG, sinalSEG)' ')' ')' ])
% manual plot attempt
plot(genvarname(sprintf('MPDF_%d%d%d%d%d%d%d%d%d', tipoQSG, gradienteQSG, tipoSIS, quantidadeSIS, concentracaoSIS, sinaisSIS, aleatorizacaoSIS, tipoSEG, sinalSEG)))
hold on
% Try to see if the variable name might be wrong
eval(['sprintf(''MPDF_%d%d%d%d%d%d%d%d%d'', tipoQSG, gradienteQSG, tipoSIS, quantidadeSIS, concentracaoSIS, sinaisSIS, aleatorizacaoSIS, tipoSEG, sinalSEG)' ]) == MPDF_111111111
end
end
end
end
end
end
end
end
end
Why does it returns me a vector for the mean(MPDF_111111111), but a single value for the for the eval equivalent? How can I remedy this problem?
Thanks!

2 comentarios

Igor de Britto
Igor de Britto el 25 de Mzo. de 2012
Managed to get MPDF_111111111 and the loop variables on the question!
Stephen23
Stephen23 el 15 de Oct. de 2014
In case the message got lost: DO NOT USE EVAL for calling trivial functions like sprintf. This is a topic that has been discussed a thousand times before...
  • Eval isn't fast, it is slower than just calling any function directly.
  • Eval obscures the code intent. Totally.
  • Eval is not compiled for optimized running. Every call has to eval all over again!
  • Eval makes debugging almost impossible.
  • Eval can produce different outputs in normal and debug modes.
  • Eval can create and overwrite variables in workspaces.
  • Eval is often associated with other practices that are not an efficient use of MATLAB... sequential variable names, for example.
These topics have been covered many times in MATLAB's official documentation, blogs and other discussions:

Iniciar sesión para comentar.

 Respuesta aceptada

Daniel Shub
Daniel Shub el 25 de Mzo. de 2012

0 votos

I don't really understand your question and we cannot run your code ...
It looks like:
plot(genvarname(sprintf('MPDF_%d%d%d%d%d%d%d%d%d', tipoQSG, gradienteQSG, tipoSIS, quantidadeSIS, concentracaoSIS, sinaisSIS, aleatorizacaoSIS, tipoSEG, sinalSEG)))
is trying to plot a string
plot('MPDF_111111111')
is odd.
Here
eval(['sprintf(''MPDF_%d%d%d%d%d%d%d%d%d'', tipoQSG, gradienteQSG, tipoSIS, quantidadeSIS, concentracaoSIS, sinaisSIS, aleatorizacaoSIS, tipoSEG, sinalSEG)' ]) == MPDF_111111111
you seem to be comparing
eval('MPDF_111111111') == MPDF_111111111
which again is odd.
Overall I guess my answer is: DON'T USE EVAL!!!!
It makes debugging a pain.
Taking another look: Are you trying to do
eval('plot(mean(MPDF_111111111))')

5 comentarios

Igor de Britto
Igor de Britto el 25 de Mzo. de 2012
Sorry. I forgot the code is not runnable without defining the min and maxes for the loop variables.
try adding this to the code:
tipoQSGmin = 1;
tipoQSGmax = 1;
gradienteQSGmin = 1;
gradienteQSGmax = 1;
tipoSISmin = 1;
tipoSISmax = 1;
quantidadeSISmin = 1;
quantidadeSISmax = 1;
concentracaoSISmin = 1;
concentracaoSISmax = 1;
sinaisSISmin = 1;
sinaisSISmax = 1;
aleatorizacaoSISmin = 1;
aleatorizacaoSISmax = 1;
tipoSEGmin = 1;
tipoSEGmax = 4;
sinalSEGmin = 1;
sinalSEGmax = 1;
Also, I didn't post the MPDF_111111111 on the question for lack of remaining characters. Sorry about that, but it's on the comments.
But you're right about what I tried to do. I want to change the name of the plotted variable, as you stated.
Also, I tried to use structured data, but things went bad because of the size of the produced matrices and the time needed to perform the tasks (there would be more than 300 matrices of 1000 x 50, so it takes forever to append the newly generated matrices by the end of the loops).
Any ideas on how to make the name of the variable change?
Daniel Shub
Daniel Shub el 25 de Mzo. de 2012
"(there would be more than 300 matrices of 1000 x 50, so it takes forever to append the newly generated matrices by the end of the loops)." That sounds like a preallocation (or lack of) issue. If you make the matrix the correct size at the beginning (cf zeros and nan), you shouldn't have that problem.
Igor de Britto
Igor de Britto el 25 de Mzo. de 2012
I'll try it now. I thought that Matlab would always rearrange the whole matrix, whether the index already exist or not.
Igor de Britto
Igor de Britto el 28 de Mzo. de 2012
Thanks, man. Took me a while to understand how to preallocate, but it worked just fine!
Jan
Jan el 28 de Mzo. de 2012
@Igor: Do not forget Daniel's suggestion not to use EVAL.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 25 de Mzo. de 2012

Comentada:

el 15 de Oct. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by