renaming a variable, or creating a dynamic variable name
102 views (last 30 days)
Show older comments
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
Stephen23
on 3 Nov 2021
Edited: Stephen23
on 4 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.
Accepted Answer
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
You would still have to access each table by a number rather than a sample name, but this avoids the dynamic naming problem.
0 Comments
More Answers (1)
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)
T2.sampleName = repmat("Table 2", height(T1), 1)
T = [T1; T2]
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", :)
T4 = T(matches(T.sampleName, 'Table 2'), :)
0 Comments
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!