Name an array using strcat

4 views (last 30 days)
Steveo
Steveo on 26 Feb 2016
Edited: Stephen23 on 19 Jun 2019
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 Comment
Stephen23
Stephen23 on 26 Feb 2016
Edited: Stephen23 on 19 Jun 2019
"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.

Sign in to comment.

Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!