Name a variable using an already existing variable

Hello,
I am working on a project consisting of different tables containing data from different patients. They are stored in different excel sheets name PPMI001i, PPMI002i, PPMI003i, and so forth. I am working on a script to let it run all the data processing I want to do with the data of one patient. To do that i assigned a global variable.
global Nr
Nr = "PPMI002i";
After that i want to extract the date from the right sheet, using readtable.
T = readtable('BSA_Daten_Test.xlsx',opts,'sheet',Nr)
Unrecognized function or variable 'opts'.
However, I would like "T" to be named after my first variable, so it does not get overwritten when I change "Nr".
I hope someone can help me with this Question.
Thank you in advance! :)

1 comentario

Stephen23
Stephen23 el 30 de Oct. de 2022
Editada: Stephen23 el 30 de Oct. de 2022
"However, I would like "T" to be named after my first variable..."
... which forces you into writing slow, complex, inefficient, obfuscated, insecure code:
"...so it does not get overwritten when I change "Nr"."
The standard simple and efficient approach is to use indexing. You should use indexing too.

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 26 de Oct. de 2022
Creating names of variables dynamically is one of the question, asked most frequently by beginners in this forum. The experts give always the same answer: Don't do this. See: TUTORIAL: Why and how to avoid Eval .
Do not store useful information in the name of the variable, but as data. This makes it easy to use this information later on. Solutions:
T.Data = readtable('BSA_Daten_Test.xlsx',opts,'sheet',Nr)
T.Patient = Nr;
Sometimes dynamic field names help also, but this makes it harder to procerss the data usually:
T.(Nr) = readtable('BSA_Daten_Test.xlsx',opts,'sheet',Nr)
The above methods allows you to create arrays:
T(1).Data = readtable('BSA_Daten_Test.xlsx',opts,'sheet',Nr)
T(1).Patient = Nr;
T(2).Data = readtable('BSA_Daten_Test.xlsx',opts,'sheet', anotherNr)
T(2).Patient = anotherNr;
This is an advantage when processing a group of patient: You can simply run a loop over all data, which works in genereal without knowing the list of names in advance. This is not possible, if the data are kept in variables called PPMI002i, PPMI003i, PPMI004r, ...

3 comentarios

Marcel-Maximilian
Marcel-Maximilian el 26 de Oct. de 2022
Editada: Marcel-Maximilian el 26 de Oct. de 2022
Thank you for your answer!
So i am having a hard time understanding your solutions. But maybe I have to give more informations.
I dont understand how the first solution would help me because it would just copy the already existing inforamtion.
In case I would run a loop over the data in the excel sheet. Like the one below. (I know it doesnt work, because i dont know how to define it correctly)
for x = 1:8 (because there are 8 patients)
T = readtable('BSA_Daten_Test.xlsx',opts,'sheet',x)
end
How do I prevent MATLAB from overwritting my table T? For example by letting him output T1, T2, T3
or what I had in mind T_PPMI002i and so forth.
I am sorry if my questions might seem a bit stupid, but I am kinda lost right now.
P.S.: I just realized the loop does work but like I exspected it overwrites my table 8 times.
Matt J
Matt J el 26 de Oct. de 2022
Editada: Matt J el 26 de Oct. de 2022
For example,
for i = 1:8
T{i} = readtable('BSA_Daten_Test.xlsx',opts,'sheet',i);
end
Thx :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 26 de Oct. de 2022

Editada:

el 30 de Oct. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by