Using Strcat to create dynamic variable names

前端 未结 3 1841
甜味超标
甜味超标 2020-12-21 15:10

I have a process which is repeated on a set of data stored in separate folders. Each time a certain folders data is processed I need new variable names as I need to results

相关标签:
3条回答
  • 2020-12-21 15:43

    The MATLAB function genvarname does what you want. In your case it would look something like:

    eval(genvarname('Set_', who)) = zeros(dim, number);
    

    However, I would follow the recommendations of previous answers and use a cell or struct to store the results.

    0 讨论(0)
  • 2020-12-21 15:44

    I'd not do it like this. It's a bad habit, it's better to use a cell array or a struct to keep multiple sets. There is a small overhead (size-wise) per field, but it'll be a lot easier to maintain later on.

    If you really, really want to do that use eval on the string you composed.

    0 讨论(0)
  • 2020-12-21 15:47

    This sort of pattern is considered harmful since it requires the eval function. See one of the following for techniques for avoiding it:

    • http://www.mathworks.co.uk/support/tech-notes/1100/1103.html
    • http://blogs.mathworks.com/loren/2005/12/28/evading-eval/

    If you insist on using eval, then use something like:

    eval(sprintf('Set_1%d = zeros(dim, number);', number))
    
    0 讨论(0)
提交回复
热议问题