Explanation of the following eval expression
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jacek Soroko
el 25 de En. de 2021
Hello everyone,
I am trying to understand how these two lines below works. Would appreciate if someone could help.
eval(['cd(''',filePath,''')'])
&
eval(['cd(''',cdold,''')']);
These two lines are taken from the following code:
...
cdold = cd;
[file, filePath] = uigetfile('lkDAT*.m','Please select a file');
eval(['cd(''',filePath,''')']); data = strtok(file,'.');
[LNK, dist, CSA, VNL, VNCalc, tn] = feval(data);
fprintf( '\nRead data from m-file %s',data);
eval(['cd(''',cdold,''')']);
fd=fopen(outputFile,'wt');
fprintf(fd,'\n*** %s - line parameters ***', outputFile);
fprintf(fd,'\n Date: %5d-%2d-%2d hour. %2d, %2dmin, %2.0fs',clock);
fprintf(fd,'\nRead data from m-file: %s', data);
...
Thanks.
1 comentario
Stephen23
el 26 de En. de 2021
Editada: Stephen23
el 26 de En. de 2021
"Explanation of the following eval expression"
Totally pointless eval usage by someone who does not understand MATLAB basics (i.e. command vs. function syntax).
Totally pointless cd when it is much more efficient to use absolute/relative filenames (which anyone who has progressed past the "MATLAB learner"-stage would know).
Goodness gracious me... this code is writen in a book: "Analiza w Matlabie stanów ustalonych i zwarciowych systemów elektroenergetycznych"! This code indicates that its author does not know how to write simple, efficient MATLAB code. No wonder MATLAB suffers from the reputation of being slow, when this is the appalling standard of code that is being taught to well-meaning students who diligently buy such books.
Please add your own reviews to online retailers selling this book, warning others from learning from this abomination.
Respuesta aceptada
the cyclist
el 25 de En. de 2021
Editada: the cyclist
el 25 de En. de 2021
The first, and most important, thing to mention here is that eval is almost never the best coding solution. Just as you are experiencing now, it typically obscures what one is trying to do.
Next, I'll note that those two lines of code are equivalent to
cd(filePath)
and
cd(cdold)
The cd command takes a character array as input, and goes to the corresponding directory. Since filePath and cdold are character arrays, they can be used directly as input.
Lastly, I will describe what the author of that code appeared to be doing, only because it can be useful in other contexts (but hopefully never using eval). What they are doing is creating one long character array, by concatenating individual char arrays. This syntax
['cd(''',filePath,''')']
is putting together three individual char arrays. The reason for the seemingly awkward use of
'cd('''
is because the author wanted a single quote in their char array. Supposed the filepath is '/Users/MATLAB/Noob'. So, they are splicing together
cd('
and
/Users/MATLAB/Noob
and
')
into the one longer string, which will be executed by the eval command.
It's an abomination, and yet another excellent example of why eval should not be used. Please go educate whoever you got this code from.
Más respuestas (1)
John D'Errico
el 25 de En. de 2021
Editada: John D'Errico
el 25 de En. de 2021
Lol. Whoever wrote the code should learn to use MATLAB, as this is just an abuse of eval, for absolutely no good reason.
For example, this:
eval(['cd(''',cdold,''')']);
is equivalent to the far simpler line of code:
cd(cdold)
All that line of code does is change the current directory, using cd.
Likewise,
eval(['cd(''',filePath,''')'])
is equivalent to
cd(filepath)
Sorry, but anyone writing code like that should have their license revoked. :-) :-)
6 comentarios
Stephen23
el 26 de En. de 2021
@Jacek Soroko: I tried searching for the title that you gave, but cound not find an exact match. Could you please write the full, correct title of this book and the author's name, so that internet searches have a chance to find this thread.
Ver también
Categorías
Más información sobre Simulink Environment Customization en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!