Make a table with variable names which are saved as strings.

23 vues (au cours des 30 derniers jours)
Myung Hyun Jo
Myung Hyun Jo le 6 Juin 2021
Let's assume that there are data files having a column of nubmers.
abc.dat
cdc.dat
usa.dat
I'd like combine them into a table.Can I use the file names as variable names?
ex) table T =
abc cdc usa
10 100 5
20 200 10
30 300 15
40 400 20
50 500 25
Matlab table is great for handling my data, but I am struggling to name variables.
I was looking at the 'eval' function, but it was not recommended from many thread here.

Réponse acceptée

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 6 Juin 2021
filename = {'abc', 'bcd', 'cde'};
A = magic(3);
T = array2table(A, 'variablenames', filename);
  2 commentaires
Myung Hyun Jo
Myung Hyun Jo le 6 Juin 2021
Thank you!
Sulaymon Eshkabilov
Sulaymon Eshkabilov le 6 Juin 2021
Most Welcome!

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 6 Juin 2021
dinfo = dir('*.dat');
filenames = {dinfo.name};
T = table();
for K = 1 : length(filenames)
thisfile = filenames{K};
[~, varname, ~] = fileparts(thisfile);
thisdata = load(thisfile, '-ascii');
T.(varname) = thisdata;
end

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by