Storing variables in for loop
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello! I am trying to create a for loop to store 16 different .csv files. The issue I am running into is that the for loop is only saving the last .csv file (file 16). I understand that the for loop is overwriting the "forcedData" variable with every iteration but I've tried using:
forcedData(k) = csvread(fileName,3,0);
and
forcedData(:,k)= csvread(fileName,3,0);
But neither of these work due to the number of elements being different on each side. I am fairly new to MATLAB so if anyone knows how to fix this issue so that I may have 16 different data sets in the forcedData variable, please help! I have included my scipt so far below this line:
for k = 1:16
%importing csv files
if k <=16
fileName = sprintf('forced_%d.csv',k);
end
if isfile(fileName)
forcedData(:,k)= csvread(fileName,3,0);
else
fprintf('File %s does not exist.\n',fileName);
end
end
1 commentaire
Réponses (1)
KSSV
le 24 Mar 2022
csvFiles = dir('*.csv') ;
N = length(csvFiles) ;
iwant = cell(N,1) ;
for i = 1:N
iwant{i} = csvread(csvFiles(i).name) ;
end
0 commentaires
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!