How to store several tables in one table using a loop

2 vues (au cours des 30 derniers jours)
Tamara Szecsey
Tamara Szecsey le 11 Nov 2020
Commenté : Tamara Szecsey le 27 Déc 2020
I want to store several csv files into tables which I then store in one table in order to plot them easily. Storing one csv file into one table works perfectly. Matlab won't use my counter for the for-loop as table fieldname, so it overwrites the first entry. This is my code so far:
counter = 1;
filename = ['measurement' sprintf( '%03d', counter) '.csv'];
while exist(filename, 'file') == 2
filename = ['measurement' sprintf( '%03d', counter) '.csv']
T.counter = readtable(filename) % this isn't working
counter = counter + 1;
end
The output is a 1x1 table T with the csv content named 'counter'. What I'd like to have is a n x 1 table T with n equals the amount of csv files.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 11 Nov 2020
Modifié(e) : Ameer Hamza le 11 Nov 2020
Creating a table of tables might not be a good strategy. The usual way is to create a cell array of tables. The following code shows an example
files = dir('measurement*.csv');
T = cell(size(files));
for i = 1:numel(files)
T{i} = readtable(files(i).name) % this isn't working
end
  5 commentaires
Ameer Hamza
Ameer Hamza le 27 Déc 2020
Write it like this
files = dir('*/measurement*.csv');
Tamara Szecsey
Tamara Szecsey le 27 Déc 2020
Even though the pathname is correct, the same error message occurs.
But it seems that I figured out how to plot several files of different directories by creating a new script for every directory and running the script in between hold on and hold off:
clc;
clear;
close all;
figure;
grid on;
hold on;
run('script1.m')
for i = 1:size(T)
Tnew{i,1}=T{i,1}
end
run('script2.m')
for i = 1:size(T)
Tnew{i,2}=T{i,1}
end
[r,c] = size(Tnew)
for j = 1:r
plot(t,Tnew{j,1}.(3), '-k')
end
hold off;
And script1.m and script2.m looks like your first answer just with a cd command in the beginning and cd ../ for exiting the directory again.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Tags

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by