genvarname command removed or not working?

조회 수: 9 (최근 30일)
D
D 2014년 12월 29일
답변: Philip Borghesani 2014년 12월 29일
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.

답변 (2개)

Jan
Jan 2014년 12월 29일
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 2014년 12월 29일
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.

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by