Here is the code:
file_name = 'test';
eval(['file_length = length(who(''-file'',' file_name '));']);
But this doesn't work, because who requires a quoted string as a variable. Then I tried this,
eval(['file_length = length(who(''-file'', ''file_name'' ));']);
It didn't work, apparently. Now file_name showed up instead of the value I put in it. This sounds a very simple question, but I just don't know what to do. Help!

 Respuesta aceptada

Andrew Newell
Andrew Newell el 9 de En. de 2012

0 votos

This is a working example:
clear
x = zeros(10,1); y = x;
save matlab.mat
file_name='matlab.mat';
eval('file_length = length(who(''-file'',file_name))')
file_length =
2
EDIT: Although I answered the question you asked, I agree with Sean and the Cyclist that it would be better not to use eval. See Loren's blog for some reasons.

3 comentarios

Qianyu
Qianyu el 9 de En. de 2012
Thank you. That worked. The reason I use eval is because this is a loop, and a series of files will go through the loop. I escaped the loop part so I didn't have to type many lines. Sorry for the unclarity.
Andrew Newell
Andrew Newell el 9 de En. de 2012
Actually, the Cyclist's solution would also work nicely in a loop.
the cyclist
the cyclist el 10 de En. de 2012
For example, even if you need the value of the looping variable in the file name, you can often use num2str() to convert the numeric to a string, and append it to the file name.

Iniciar sesión para comentar.

Más respuestas (2)

Sean de Wolski
Sean de Wolski el 9 de En. de 2012

2 votos

Why use eval? Nothing in that statement looks like it couldn't be done without it.
the cyclist
the cyclist el 9 de En. de 2012

2 votos

Agree with Sean that you do not need eval() here. Try this:
file_name = 'test';
file_length = length(who('-file',file_name));

Etiquetas

Preguntada:

el 9 de En. de 2012

Editada:

el 16 de Feb. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by