Borrar filtros
Borrar filtros

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 visualización (últimos 30 días)
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 comentario
Stephen23
Stephen23 el 1 de Jul. de 2021
Editada: Stephen23 el 1 de Jul. de 2021
"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.

Iniciar sesión para comentar.

Respuesta aceptada

Steven Lord
Steven Lord el 1 de Jul. de 2021
A tl;dr version of Stephen's comment:
Can you do this? Yes.
Should you do this? The general consensus is no.
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)
label = struct with fields:
Data: [4×4 double] ID: 35 Name: 'P69R10'
x = label.Data
x = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by