renaming a variable, or creating a dynamic variable name

19 views (last 30 days)
Teshan Rezel
Teshan Rezel on 3 Nov 2021
Edited: Chris on 30 Nov 2021
Hi folks,
I read on here that creating dynamic variable names is bad programming practice, so was hoping to get some help with a problem I'm facing.
I have a cell array containing 54 tables. Each table has a unique name associated with it which is not present in the cell array or data table. It is recorded in a string cell called "sampleNames".
I need to split each table in the cell array into its own table, with each table having the name of the sample it is related to. For example:
sampleAAA = data{1, 1};
...
...
...
sampleZZZ = data{1, n};
where "data{}" is the cell array containing all my tables, and "sampleAAA" would be sampleNames(1).
Is there a way to do this?
Thanks in advance!
  3 Comments
Stephen
Stephen on 3 Nov 2021
"I need to split them because I need to analyse each table individually"
I doubt that you need to split your data into separate variables just to analyse them.
"...doing so in the current state of the data would be incredibly messy and convoluted, leading to errors."
Really, why?
You can trivially access the names and the data using the same indices, I don't see any reason why that would have to be "messy and convoluted". In contrast, the approach you are attempting is more complex and much less efficient than indexing.
Your mistake is mixing meta-data with code: best avoided if you want to write simple, robust, efficient code.

Sign in to comment.

Accepted Answer

Chris
Chris on 3 Nov 2021
Edited: Chris on 30 Nov 2021
If the cells are really holding Matlab tables, you could assign the sampleNames as table Descriptions:
for idx = 1:n
data{1,idx}.Properties.Description = sampleNames(idx);
end
If they're not tables, how about using a struct array?
data = repmat({rand(4)},1,2);
sampleNames = ["sampleAAA";"sampleAAB"];
for idx = 1:numel(sampleNames)
allTables(idx).name = sampleNames(idx);
allTables(idx).data = data(1,idx);
end
allTables
allTables = 1×2 struct array with fields:
name data
You would still have to access each table by a number rather than a sample name, but this avoids the dynamic naming problem.

More Answers (1)

Steven Lord
Steven Lord on 3 Nov 2021
If the elements of sampleNames are valid MATLAB identifiers I think Walter Roberson's suggestion to store each table in a field of a struct array seems reasonable. Each element in sampleNames would give the name for the field containing the corresponding table array.
Another possibility is to concatenate the table arrays together after adding one variable to each array, call it sampleName. Each row of this new variable would contain the name from sampleName of the table that contributed the row to the larger table.
load patients
VN = {'Name', 'Height', 'Weight'};
T1 = table(LastName(1:5), Height(1:5), Weight(1:5), 'VariableNames', VN);
T2 = table(LastName(6:10), Height(6:10), Weight(6:10), 'VariableNames', VN);
T1.sampleName = repmat("Table 1", height(T1), 1)
T1 = 5×4 table
Name Height Weight sampleName ____________ ______ ______ __________ {'Smith' } 71 176 "Table 1" {'Johnson' } 69 163 "Table 1" {'Williams'} 64 131 "Table 1" {'Jones' } 67 133 "Table 1" {'Brown' } 64 119 "Table 1"
T2.sampleName = repmat("Table 2", height(T1), 1)
T2 = 5×4 table
Name Height Weight sampleName __________ ______ ______ __________ {'Davis' } 68 142 "Table 2" {'Miller'} 64 142 "Table 2" {'Wilson'} 68 180 "Table 2" {'Moore' } 68 183 "Table 2" {'Taylor'} 66 132 "Table 2"
T = [T1; T2]
T = 10×4 table
Name Height Weight sampleName ____________ ______ ______ __________ {'Smith' } 71 176 "Table 1" {'Johnson' } 69 163 "Table 1" {'Williams'} 64 131 "Table 1" {'Jones' } 67 133 "Table 1" {'Brown' } 64 119 "Table 1" {'Davis' } 68 142 "Table 2" {'Miller' } 64 142 "Table 2" {'Wilson' } 68 180 "Table 2" {'Moore' } 68 183 "Table 2" {'Taylor' } 66 132 "Table 2"
If then you needed to process each table's data separately you could use groupsummary or grouptransform with sampleName as the grouping variable. Or you could select rows using that variable:
T3 = T(T.sampleName == "Table 1", :)
T3 = 5×4 table
Name Height Weight sampleName ____________ ______ ______ __________ {'Smith' } 71 176 "Table 1" {'Johnson' } 69 163 "Table 1" {'Williams'} 64 131 "Table 1" {'Jones' } 67 133 "Table 1" {'Brown' } 64 119 "Table 1"
T4 = T(matches(T.sampleName, 'Table 2'), :)
T4 = 5×4 table
Name Height Weight sampleName __________ ______ ______ __________ {'Davis' } 68 142 "Table 2" {'Miller'} 64 142 "Table 2" {'Wilson'} 68 180 "Table 2" {'Moore' } 68 183 "Table 2" {'Taylor'} 66 132 "Table 2"

Community Treasure Hunt

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

Start Hunting!