How would I import multiple data sets with the same variables

11 views (last 30 days)
I would like to be able to download mutiple data sets that all have the same colums just differnet values, and put them into their own variables. Is there a simpler way of doing this than what I'm currently doing? Here's what I have right now, which downloads each of the data sets as their own variable, then grabs the columns I want and sets those as their own variable. Right now I only have 2 of the files downloaded, but I'll have about 8 when I'm done collecting data.
%% 16 L/h
data1 = readtable('File1.txt');
File1 = flip(data1);
T2_find = File1{:,3};
A = find(T2_find > 1);
B = min(A);
NewFile1 = File1(B:end,:);
T1_File1 = NewFile1{:,2};
T2_File1 = NewFile1{:,3};
T3_File1 = NewFile1{:,4};
T4_File1 = NewFile1{:,6};
F_File1 = NewFile1{:,5};
R_File1 = NewFile1{:,7}; % Illuminance [kW/m^2]
A_File1 = NewFile1{:,12}; % Area [m^2]
dT_File1 = NewFile1{:,9};
cp_File1 = NewFile1{:,11}; % heat capacitance [kJ/kgK]
m_File1 = 16 * (1/60) * 12; % [kg]
E_in_File1 = R_File1 .* A_File1;
E_abs_File1 = m_File1 .* cp_File1 .* dT_File1;
%% 14 L/h
data2 = readtable('File2.txt');
File2 = flip(data2);
T2_find2 = File2{:,3};
A2 = find(T2_find2 > 1);
B2 = min(A2);
NewFile2 = File2(B2:end,:);
T1_File2 = NewFile2{:,2};
T2_File2 = NewFile2{:,3};
T3_File2 = NewFile2{:,4};
T4_File2 = NewFile2{:,6};
F_File2 = NewFile2{:,5};
R_File2 = NewFile2{:,7}; % Illuminance [kW/m^2]
A_File2 = NewFile2{:,12}; % Area [m^2]
dT_File2 = NewFile2{:,9};
cp_File2 = NewFile2{:,11}; % heat capacitance [kJ/kgK]
m_File2 = 16 * (1/60) * 12; % [kg]
E_in_File2 = R_File2 .* A_File2;
E_abs_File2 = m_File2 .* cp_File2 .* dT_File2;
Thanks for any help!

Answers (1)

Voss
Voss on 27 Feb 2024
"put them into their own variables"
It's better to leave them in the tables. Rather than creating, say, 72 (= 9 variables per table * 8 tables) separate variables in your workspace, you can have everything in one variable: a cell array that contains all the tables, or a big table constructed from all the tables.
% generate a set of file names, e.g., using dir()
% (note that these may not be in the order you want):
files = dir('File*.txt');
filenames = fullfile({files.folder},{files.name});
% initialize a cell array C to store the tables:
N = numel(filenames);
C = cell(N,1);
% define your variables that don't depend on the file:
m = 16 * (1/60) * 12; % [kg]
% loop over the files:
for ii = 1:N
% read the file and vertically flip the table:
data = flip(readtable(filenames{ii}),1);
% only keep the rows from the first row with column 3 > 1:
idx = find(data{:,3} > 1, 1);
if isempty(idx) % if no row has column 3 > 1,
idx = size(data,1)+1; % keep an empty table
end
data = data(idx:end, :);
% calculate and store some new table variables:
data{:,end+1} = data{:,7} .* data{:,12};
data{:,end+1} = m .* data{:,11} .* data{:,9};
% store the table in the cell array:
C{ii} = data;
end
% if you want to put all those tables together in a single table:
T = vertcat(C{:});

Categories

Find more on Large Files and Big Data 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!