Effacer les filtres
Effacer les filtres

Export to excel from matlab

11 vues (au cours des 30 derniers jours)
Sofia Santos
Sofia Santos le 6 Mai 2020
Commenté : Sofia Santos le 6 Mai 2020
Hello, I have a script that runs all the text filles, that containes a signal, in one folder and calculates the amplitude for all.
I want so save those amplitudes in excel, but when I tried it only saves the amplitude of the last signal, like in the image, instead of all the amplitudes for all the signals.
I hope you can help me, thank you very much!
paste = '/Users/sofiasantos/Desktop/sinais'; %directory
for k=1:numel(data)
data_org = importdata(signal,'\t',7); %text file with 7 columms
EDA= EDA(:,6); %I only want the 6 collum of the text file
EDAuS=((EDA/2^10)*3)/0.12; %convert to uS
N= length(EDAuS);
fs = 1000;
t = (0:N-1)/fs; %time
f = (0:N-1)/T;
[b,a] = butter(6,0.0035,'low');
EDAuS_filter = filtfilt(b,a,EDAuS); %filtering signal EDA
minimo= min(EDAuS_filter);
maximo= max(EDAuS_filter);
disp('amplitude='); disp(amp); %amplitude da SCR
%% Export to excel
T = table(amp,'RowNames',{signals});

Réponse acceptée

Guillaume le 6 Mai 2020
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:
%your pre-loop code unchanged:
paste = '/Users/sofiasantos/Desktop/sinais'; %directory
%create array to store all the amp:
amp_all = zeros(numel(data), 1);
for k=1:numel(data)
%.. your loop code unchanged except for the last 3 lines that are removed and the addition of
amp_all(k) = amp;
T = table(amp_all, 'RowNames', {data.name}, 'VariableNames', {'amp'});
writetable(T, 'signals_EDA.xlsx', 'WriteRowNames', true); %not much point in giving names to the rows if you don't write them to the excel file
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:
%most of the code unchanged
paste = '/Users/sofiasantos/Desktop/sinais'; %directory
for k=1:numel(data)
%... until it comes to write/create the table:
T = table(amp,'RowNames',{signals}); %note that signals is undefined in your code. Did you mean signal (without an s)
dowriteheader = k == 1; %only write header for the first file
destination = sprintf('A%d', k+1); %row 1 is header, therefore data goes into row k+1.
writetable(T, 'signals_EDA.xlsx', 'WriteVariableNames', dowriteheader, 'WriteRowNames', true, 'Range', destination);
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.
  1 commentaire
Sofia Santos
Sofia Santos le 6 Mai 2020
Solved my problem :)
Thank you so much for your help and for such a detailed explanation!!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by