How can add a name of table by concatenating strings?

7 vues (au cours des 30 derniers jours)
Zeynab Mousavikhamene
Zeynab Mousavikhamene le 31 Jan 2020
Modifié(e) : Stephen23 le 6 Fév 2020
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 commentaires
BN
BN le 31 Jan 2020
Modifié(e) : BN le 31 Jan 2020
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 le 1 Fév 2020
"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:

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 31 Jan 2020
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 commentaires
Walter Roberson
Walter Roberson le 6 Fév 2020
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 le 6 Fév 2020
Modifié(e) : Stephen23 le 6 Fév 2020
"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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by