Table to Array conversion using a for loop
Show older comments
Hi! Is there a way I can do this operation within a for loop?
load 'temps-2021-11-16.mat'
temps_2021_11_16 = table2array(data(:,:));
load 'temps-2021-10-31.mat'
temps_2021_10_31 = table2array(data(:,:));
load 'temps-2021-10-15.mat'
temps_2021_10_15 = table2array(data(:,:));
load 'temps-2021-09-13.mat'
temps_2021_09_13 = table2array(data(:,:));
load 'temps-2021-09-29.mat'
temps_2021_09_29 = table2array(data(:,:));
load 'temps-2021-08-12.mat'
temps_2021_08_12 = table2array(data(:,:));
load 'temps-2021-07-27.mat'
temps_2021_07_27 = table2array(data(:,:));
load 'temps-2021-04-06.mat'
temps_2021_04_06 = table2array(data(:,:));
load 'temps-2021-03-21.mat'
temps_2021_03_21 = table2array(data(:,:));
load 'temps-2021-03-05.mat'
temps_2021_03_05 = table2array(data(:,:));
load 'temps-2021-02-17.mat'
temps_2021_02_17 = table2array(data(:,:));
Thank you so much!
1 Comment
temps_2021_02_17 = ..
% ^^^^^^^^^^ do not force meta-data into variable names
unless you really want to force yourself into writing slow, complex, inefficient, obfuscated, insecure, buggy code that is hard to debug:
Those files are so neatly designed with one variable name (i.e. 'data'), it really would be such a shame to awkwardly force meta-data into variable names. Indexing is simpler and much more efficient.
Accepted Answer
More Answers (1)
A much better approach stores the imported data in the structure returned by DIR:
P = 'absolute or relative path to where the files are saved';
S = dir(fullfile(P,'*.mat'));
for k = 1:numel(S)
F = fullfile(P,S(k).name);
T = load(F);
S(k).data = table2array(T.data);
end
All of the imported data will be available in the structure S, for example the 2nd file:
S(2).name
S(2).data
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!