Name an array using strcat

4 Ansichten (letzte 30 Tage)
Steveo
Steveo am 26 Feb. 2016
Bearbeitet: Stephen23 am 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 Kommentar
Stephen23
Stephen23 am 26 Feb. 2016
Bearbeitet: Stephen23 am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by