Loading multiple data folders and call them in a For Loop

2 views (last 30 days)
Jeroen van Bemmel
Jeroen van Bemmel on 19 Nov 2021
Commented: Stephen on 19 Nov 2021
I have 13 data files named data24,data30......to data99
I want to read all the data sets by calling them one for one in a for loop to extract data from each file. By selecting one vector of each file
I have tried some num2str, text2double commands but nothing seems to get it
i have:
data24 = readtable('c4-40-steady_15_pressure24.dat');
data30 = readtable('c4-40-steady_15_pressure30.dat');
data35 = readtable('c4-40-steady_15_pressure35.dat');
data40 = readtable('c4-40-steady_15_pressure40.dat');
data50 = readtable('c4-40-steady_15_pressure50.dat');
data60 = readtable('c4-40-steady_15_pressure60.dat');
data70 = readtable('c4-40-steady_15_pressure70.dat');
data80 = readtable('c4-40-steady_15_pressure80.dat');
data85 = readtable('c4-40-steady_15_pressure85.dat');
data90 = readtable('c4-40-steady_15_pressure90.dat');
data95 = readtable('c4-40-steady_15_pressure95.dat');
data98 = readtable('c4-40-steady_15_pressure98.dat');
data99 = readtable('c4-40-steady_15_pressure99.dat');
Section = [24 30 35 40 50 60 70 80 85 90 95 98 99]; %Sections along blade
for i = Section
for j = 1:length(Section)
x_pos(j,:) = table2array(data('here is my problem, i want the numbers to be read')(2:62,1));
end
end

Answers (2)

Mathieu NOE
Mathieu NOE on 19 Nov 2021
hello
see example below for excel files - simply adapt the inner code to read your data and file extension
also you need to load this excellent FEX submission
clc
clearvars
fileDir = pwd;
S = dir(fullfile(fileDir,'data00*.xlsx')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order , see :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
for k = 1:length(S)
S(k).data = importdata( fullfile(fileDir, S(k).name));
end
  1 Comment
Stephen
Stephen on 19 Nov 2021
Note that 'data' was part of the variable names. Matching the filenames requires a file pattern something like
'c4-40-steady_15_pressure*.dat'

Sign in to comment.


Rik
Rik on 19 Nov 2021
Your mistake was in your naming of the variables. If you can guarantee they are always integers, you can do something like this (otherwise you will need ismember with a lookup table):
Section = [24 30 35 40 50 60 70 80 85 90 95 98 99]; %Sections along blade
data=cell(1,max(Section));
for n=1:numel(Section)
ind=Section(n);
filename=sprintf('c4-40-steady_15_pressure%d.dat',ind);
data{ind}readtable(filename);
end
for i = Section
for j = 1:length(Section)
x_pos(j,:) = table2array(data{j}(2:62,1));
% ^ not sure j is what you need
end
end
Also, avoid length, use numel or size instead.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by