Name an array using strcat

조회 수: 6 (최근 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