EVAL is evil? Using variables created dynamically - info retrieval
Ältere Kommentare anzeigen
Hi,
I am creating an information retrieval program that will implement the Rocchio method. This isn't important but can be relevant.
I basically have a text document with 40 documents; 20 truthful, 20 deceptive.
The documents are resolved to one word per line and these are the documents to be read. These documents are named like d_hotelname_int or t_hotelname_int
d - deceptive ... t - truth
hotelname refers to the different hotel names
int is the document number
I wanted variables to reflect the document names to avoid confusion so when I wanted to find the term frequency (TF). I want the variable to be TF_documentName.
I went about it like this:
TFdocname = sprintf('TF_%s', documentName);
eval([TFdocname) '= termFrequencies']);
output: TF_d_hilton_1 TF_d_hilton_2 ... TF_t_affinia_20
now I want to get the weight of these new variables which hold the TF
Thought Experiment:
weight = 1 + log10(TFdocname)
in theory I thought it would work but is there a way of calling these variables from eval.
1 Kommentar
- 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:
Akzeptierte Antwort
Weitere Antworten (2)
per isakson
am 9 Dez. 2012
Bearbeitet: per isakson
am 13 Okt. 2016
1 Stimme
The standard answer:
- yes, EVAL is indeed evil
- see the FAQ
- try structures with dynamical field names. Syntax: struct_name.(string_var))
Next one might say: it depends on the context
- are you making a tool that you will use and improve over some time?
- are you making a small experiment and will through away the code in a couple of days?
- are you the only one who will use the tool?
2 Kommentare
Tanil
am 9 Dez. 2012
per isakson
am 9 Dez. 2012
IMO:
- the eval-approach doesn't offer any(?) advantages over structure with dynamic field names, but disadvantages
- squeezing various information into the variable names often leads to complicated code
I guess, I would have tried to use a structure array with some appropriate fields with descriptive names.
You could make a couple of small experiments and ask for comments here.
Image Analyst
am 9 Dez. 2012
I don't quite follow what you your code is trying to do but I'm sure it can be done without using eval() - your eval statement isn't even the correct syntax because it's missing a right bracket. For example:
TFdocname = sprintf('TF_%s', documentName); % documentName is s pre-defined string.
TFdocname = 'termFrequencies'; % Overwrite TFdocname
TF_documentName = 'TF_documentName.' % TF_documentName is the name you said you wanted.
Again, I'm not really sure what you want so I can't give the correct code.
Kategorien
Mehr zu Variables finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!