Reputation: 338
Apologies this is similar to "Loop through files in a folder in matlab" where I've gotten some of the code but I have encountered a problem with this code.
I have many csv files I need to loop over and combine in to one long csv/matrix to analyse, and so I am using the code
files = dir('*.csv'); % Get all input files
for file=files' % loop over files
csv = csvread(file.name); %get data points
signal = csv(:,2);
end
The problem I've found is that this only seems to take data from the first file. As in, if:
file1 = [1 2 3];
file2 = [4 5 6];
I get signal = 1 2 3, not 1 2 3 4 5 6.
So, it's as if the loop isn't moving on from the first file, but I thought a for loop was forced to move on, hence my confusion.
TIA
Upvotes: 0
Views: 62
Reputation: 30047
You just need to loop through the files struct
files = dir('*.csv'); % Get all input files
N = numel( files );
signal = cell( N, 1 ); % preallocate output
for ifile = 1:N % loop over files
csv = csvread( file(ifile).name ); % get data points
signal{ifile} = csv(:,2); % store output
end
Then you might combine all the results if you want to operate on them as one
signal = vertcat( signal{:} );
Upvotes: 2
Reputation: 338
I got there in the end
files = dir('*.csv'); % Get all input files
L = length(files);
csv = csvread(files(1).name);
signal = csv(:,2);
for i = 2:L
csv = csvread(files(i).name);
%Did stuff with code
end
Upvotes: 0