Use placeholder for table

2 views (last 30 days)
Jonas
Jonas on 24 Jul 2014
Answered: Vishnu on 12 Jul 2023
Is it possible to assign some placeholder in a script instead of a table name? Let´s say that I use certain table multiple times in a script (for example table called T), however, I know that at a later time I will need to replicate the script with only the table name changed. Therefore, I would like to replace the table name in the script with some placeholder for which the proper table name would be defined at the beginning of the script (for example something like placeholder = T), so when I run the script, it will read T instead of the placeholder. I know that it is pretty easy to do for RowNames, Variables, etc. but have been unable to figure it out for the table name itself.

Answers (2)

Steven Lord
Steven Lord on 13 Jun 2023
I know this thread is old, but in this scenario I'd create a function rather than a script. If the function accepts an input argument you can call it with whatever table you like and the name of the variable in the function workspace doesn't need to change. In the example below the input argument I pass into myfun changes, but the name of the variable in the function doesn't.
T1 = array2table(magic(4));
T2 = array2table(ones(3, 5));
T3 = array2table(randi([-10 10], 6, 2));
myfun(T1)
The input table array T1 is of size [4 4].
myfun(T2)
The input table array T2 is of size [3 5].
myfun(T3)
The input table array T3 is of size [6 2].
function myfun(theTable)
fprintf("The input table array %s is of size %s.\n", inputname(1), mat2str(size(theTable)))
end

Vishnu
Vishnu on 12 Jul 2023
Hello @Jonas
I am not able to understand what exactly you are looking for but if you want to dynamically open tables using a placeholder for path then you can look into this simillar question:- Variable as placeholder
table = "Tbl1";
path = "/Users/Vishnu/Documents/Data/"; %example path
filename = "Example" + table + ".xls"; %example path
fid = fopen(fullfile(path, filename));
If this does not resolves your issue please feel free to reply to this same thread.

Categories

Find more on Tables 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!