How can add a name of table by concatenating strings?

6 次查看(过去 30 天)
I have multiple matlab tables that at first I need load them to the matlab and then do some analysis. Assume they are saved with these names: tableA, tableB, tableC. I am interested to load all in one array by defining the name of tables in one array:
A=['tableA','tableB','tablesC']
so that I can write a for loop to load all arrays instead of writing load for each individual table. Sth like:
for i=1:size(A)
load(A(i))
end
and it wont work because A(i) are strings not the name of tables.
How can I solve this issue?
It is more genreal question. It happens to me in many cases that I need to make the name of table by strcat function.
How can I do that?
  3 个评论
BN
BN 2020-1-31
编辑:BN 2020-1-31
I'm not sure why you require to do this because TUTORIAL: Why Variables Should Not Be Named Dynamically (eval) you can probably fix your problem by store all your tables in a cell array.
D = 'D:\whatever'; % select directory
filePattern = fullfile(D, '*.mat'); %or if you have .xlsx tables
file = dir(filePattern);
x = {};
for z = 1 : numel(file)
baseFileName = file(z).name;
fullFileName = fullfile(D, baseFileName);
x{z} = load(fullFileName); %or readtable if you have .xlsx tables
fprintf('read file %s\n', fullFileName);
end
then you can simply access each table using something like:
mytable = x(:,1)
let me know if it worked for you !
Stephen23
Stephen23 2020-2-1
"It happens to me in many cases that I need to make the name of table by strcat function."
Forcing numbers or any other meta-data into variable names is a sign that you are doing something wrong:
"How can I solve this issue?"
Use arrays and indexing. You can trivially load into an output variable (which is a scalar structure):
S = load(...);
and then use indexing to allocate that array to a non-scalar structure or a cell array... and is exactly what the MATLAB documentation recommends:

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2020-1-31
Don't do that!
filenames = something appropriate ;
nfiles = length(filenames);
tcell = cell(nfiles, 1);
for k=1:nfiles
filestruct = load(filenames{k});
fn = fieldnames(filestruct);
tcell{k} = filestruct.(fn{1});
end
A = vertcat(tcell{:}) ;
  12 个评论
Walter Roberson
Walter Roberson 2020-2-6
Hmmm, my code does not add dynamic names to the structure, or at least not deliberately, but I guess it would not be completely wrong to argue that the way I use load() is a use of dynamic field names. MATLAB automatically creates the field names in that case, rather than my deliberately creating them. I read from them rather than writing to them. If the variable names in the file were fixed then even this would not be needed.
Stephen23
Stephen23 2020-2-6
编辑:Stephen23 2020-2-6
"I wanted to make dynamic name for the table which is not recommended."
Correct: accessing variable names dynamically is not recommended by experienced MATLAB users, and is also very clearly discouraged by the MATLAB documentation:
"Walter's code add dynamic name to structure not directly to the table (which is not recommended)."
I don't see that advice anywhere in the MATLAB documentation. While it is certainly possible to misuse dynamically named fields (e.g. where a non-scalar structure with indexing would be simpler and more effiicient) in general I see no reason to avoid them, especially in your case when it is the much better option.
Most of the risks/disadvantages to dynamically naming variables, such as those I list here:
do not apply to dynamically named fields, perhaps efficiency could be relevant.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by