Indeed, you will end with just the data for the last file in the excel file since for each file, you overwrite what you wrote in the excel file for the previous text file.
Probably, the best solution is to store the amp you calculate in an array that you fill one row at a time. Then once the loop has finished, convert that to a table and save it all at once in the excel file:
paste = '/Users/sofiasantos/Desktop/sinais';
data=dir(fullfile(paste,'*.txt'));
amp_all = zeros(numel(data), 1);
for k=1:numel(data)
amp_all(k) = amp;
end
T = table(amp_all, 'RowNames', {data.name}, 'VariableNames', {'amp'});
writetable(T, 'signals_EDA.xlsx', 'WriteRowNames', true);
The alternative is writing to the excel file after you've processed each file, you then have to tell writetable to write on different rows each time and make sure it only writes the header for the first file:
paste = '/Users/sofiasantos/Desktop/sinais';
data=dir(fullfile(paste,'*.txt'));
for k=1:numel(data)
T = table(amp,'RowNames',{signals});
dowriteheader = k == 1;
destination = sprintf('A%d', k+1);
writetable(T, 'signals_EDA.xlsx', 'WriteVariableNames', dowriteheader, 'WriteRowNames', true, 'Range', destination);
end
As you can see it's more complicated and is going to be slower (matlab load and saves the excel file at each iteration). The only benefit of this option is that if your code errors before the loop is finished, you have what's been processed so far written in the excel file.
0 Comments
Sign in to comment.