genvarname command removed or not working?

1 view (last 30 days)
D
D on 29 Dec 2014
Answered: Philip Borghesani on 29 Dec 2014
I'm trying to use the genvarname command for Matlab R2014b. I have a "for" loop that cycles through different strings, and I want to make a set of variables with names like "(string1)_temperature", "(string2)_temperature", etc. I thought the genvarname command would work for me, however I got some error messages. I then tried the example input from Matlab's genvarname help page:
record.(genvarname(['reading' datestr(clock, 'HHMMSS')])) = rand(1)
but I got an error here, too. I see a note saying genvarname will be removed in a future release, but it already seems to have been removed.
Can someone please point me in the right direction for a substitute for genvarname? The suggestions on the genvarname page don't seem to help me here.

Answers (2)

Jan
Jan on 29 Dec 2014
In your case you can simply omit the genvarname:
record.(['reading', datestr(clock, 'HHMMSS')]) = rand(1)
This at least not as prone to errors as eval is and it does not pollute the lookup table for variables, such that Matlab's JIT acceleration does not suffer. But as usual it is worth to mention, that hiding the data to operate on in the names of the variables (here the field names) is a bad programming style. Such complicated and indriect methods require further complicated and indirect methods to access the variables later on. This would be much better and cleaner:
record.value(k) = rand(1);
record.time(k) = clock;
This method allows e.g. a pre-allocation, such that creating a data set with 1e9 will not fragment the memory until the computer stalls completely. In addition it is trivial to find the values which belong to a certain time interval - try this when the time is coded in the field names...

Philip Borghesani
Philip Borghesani on 29 Dec 2014
genvarname has not been removed from R2014b, the code you posted works for me. genvarname.m should be in the toolbox directory [matlabroot '/toolbox/matlab/lang']. Check your path and matlab installation something is wrong.
What error message are you getting?
matlab.lang.makeValidName is a much better function for making a valid name and can be used here if needed.
You may not need such a function in the first place see Jan's answer to this question.

Tags

Community Treasure Hunt

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

Start Hunting!