Name an array using strcat

4 次查看(过去 30 天)
Steveo
Steveo 2016-2-26
编辑: Stephen23 2019-6-19
I have an array of measurement data that I want to automatically sort into categories depending on the values with the data. Measurements are either Rings or Slices depending whether PhiR = ThetaI (for a Ring) or PhiR == 180 (for a Slice). I want to move the data for each measurement into a new array / struct with a name that indicates what the measurement was i.e. "Slice_X" or "Ring_X" where X is the value of ThetaI. The only way I can see create the name is with: strcat('Slice_',num2str(ThetaI(i))), however, this causes a "Subscripted assignment dimension mismatch" when I try to use it as a name for an array.
Any thoughts of how best to do this?
Thanks
  1 个评论
Stephen23
Stephen23 2016-2-26
编辑:Stephen23 2019-6-19
"Any thoughts of how best to do this?"
Yes: don't do this.
Accessing variable names is a slow, buggy, and obfuscated way to code. I put lots of links for you to read, so you can see that many highly experienced programmers also recommend to avoid creating dynamically named variables. Beginners might think that using dynamically defined variables names is a great idea, but it will cause more problems than it solves. Read all of the links in my answer to know why.
The neatest, fastest, and most robust way to solve your task is to simply use indexing into one variable. That one variable could be an ND array, a cell array, a structure, a table...: you should pick the simplest data array that fits your data.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by