Combine The Workspace Variable Name of a unit8 Variable with Value of a 1x1 Double to Create An Output Variable with a Combined Name
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello, I wrote a code which takes two inputs: (1) Label_ID (1x1 double) and (2) Label_Name (720x1280 unit8), which extracts data from the value entered in input (1) from [x,y] coordinates from input (2) which is derrived from pixels of an image.
The input section of the code looks like this:
Label_ID=35; % **ENTER LABEL ID**
Label_Name=P69R10; % **ENTER NAME OF LABEL FILES**
... (Code to process the data)
Output (double-precision array)
For data entered like this, I would like the output variable name (which is a double-precision array) to have a name which reflects the inputs, namley the Label_Name and value of Label_ID, thus P69R10L35, where the L (for label) and value of 35 is added to the Label_Name P69R10.
Is there a way to programatically do this, so the output saves as a variable named: P69R10L35 = The Output of the Code?
Thanks so much,
Brent
댓글 수: 1
Stephen23
2021년 7월 1일
편집: Stephen23
2021년 7월 1일
"Is there a way to programatically do this..."
Of course.... but only if you want to force yourself into writing slow, inefficient, complex, obfuscated code which is liable to bugs and difficult to debug:
"...so the output saves as a variable named: P69R10L35 = The Output of the Code?"
Forcing meta-data into variable names indicates a problem in your code/data design. Meta-data is data, and data belongs in variables, not in variable names:
Once you magically create this variable, how will you access/process it? How will you even know what variables exist?
This approach is flawed in so many ways, but is easily avoided by storing meta-data in variables.
채택된 답변
Steven Lord
2021년 7월 1일
A tl;dr version of Stephen's comment:
Can you do this? Yes.
There are several different potential approaches to keeping the three pieces of information together. One way is to use a struct array.
Label_ID=35; % **ENTER LABEL ID**
Label_Name='P69R10'; % **ENTER NAME OF LABEL FILES**
label = struct('Data', magic(4), 'ID', Label_ID, 'Name', Label_Name)
x = label.Data
댓글 수: 0
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!