필터 지우기
필터 지우기

Use placeholder for table

조회 수: 3 (최근 30일)
Jonas
Jonas 2014년 7월 24일
답변: Vishnu 2023년 7월 12일
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.

답변 (2개)

Steven Lord
Steven Lord 2023년 6월 13일
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 2023년 7월 12일
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.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by