Table to Array conversion using a for loop

10 views (last 30 days)
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
Stephen23
Stephen23 on 11 Jul 2022
Edited: Stephen23 on 11 Jul 2022
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.

Sign in to comment.

Accepted Answer

Jon
Jon on 11 Jul 2022
Edited: Jon on 11 Jul 2022
I agree with @Stephen23 comments about not using dynamically named variables, here's another approach, in which you store all of the data in a single table. You can then easily retrieve data corresponding to a particular date, as shown in the end of the example code,
% get list of relevant data files
list = dir('temps-*.mat')
% loop through data files saving data into tables
tableCell = cell(numel(list),1); % cell array to temporarily hold individual tables
for k = 1:numel(list)
load(list(k).name) % load the table
% find number of rows in the table
numRows = size(data,1);
% add new first column to table with date corresponding to source file name
[~,name] = fileparts(list(k).name)
date = datetime(name(7:end))
dateColumn = table(repmat(date,numRows,1),'VariableNames',{'date'});
data = [dateColumn data]
% put table into temporary cell array
tableCell{k}= data;
end
% concatenate table to make overall table
dataTable = vertcat(tableCell{:})
% now if for example you want an array of data corresponding to
% 2021-10-31 you can use
idl = dataTable.date == '2021-10-31'
data = table2array(dataTable(idl,2:end))
  4 Comments
Jon
Jon on 14 Jul 2022
Great, glad you got it working. Good luck with your project.

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 11 Jul 2022
Edited: Stephen23 on 11 Jul 2022
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 Cell Arrays 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!