Moving data to the next column in a forloop

Hello,
I have to load 31 excel files into a script that will then be plotted. I am stuck trying to create a forloop that reads the first excel file and inserts the data into the first column of the Data_Evap variable, that then for each loop inserts the next sheets data into the next column. Im trying to create a matrix 31x31 (days x number of excel files).
Im still a beginner at using Matlab (R2020b), so apologises if this is a basic solution i am missing.
% Initalising Variables:
Info_Evap = dir('Evaporation/*.csv');
Data_Evap = [];
%Evapotransposition Forloop
for i=length(Info_Evap)
name = Info_Evap(i).name;
data = readtable(['Evaporation/',name]);
Data_Evap = [Data_Evap; data(1:end-1,2)]; %-1 is to remove the last value that needs deleted.
end
Any help or advice at all regarding setting up forloops that can make new columns for each loop woudl be greatly appreciated.

Answers (1)

% Initalising Variables:
Info_Evap = dir('Evaporation/*.csv');
Data_Evap = [];
%Evapotransposition Forloop
for i=1:length(Info_Evap)
name = Info_Evap(i).name;
data = readtable(['Evaporation/',name]);
Data_Evap = [Data_Evap, data(1:end-1,2)]; %-1 is to remove the last value that needs deleted.
end
However, this would give you trouble if the any of the variable names are the same, which I suspect is going to happen for you. I would suggest instead,
% Initalising Variables:
evfolder = 'Evaporation';
Info_Evap = dir(fullfile(evfolder, *.csv'));
filenames = fullfile({Info_Evap.folder}, {Info_Evap.name});
Data_Evap = table();
%Evapotransposition Forloop
for i = 1:length(Info_Evap)
thisfile = filenames{i};
[~, basename, ~] = fileparts(thisfile);
data = readmatrix(thisfile);
Data_Evap.(basename) = data(1:end-1,2); %-1 is to remove the last value that needs deleted.
end
This creates a table in which each variable is named according to the basic filename (without extension).

Products

Release

R2020b

Tags

Asked:

on 6 Mar 2021

Answered:

on 6 Mar 2021

Community Treasure Hunt

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

Start Hunting!