Export to excel from matlab

조회 수: 7 (최근 30일)
Sofia Santos
Sofia Santos 2020년 5월 6일
댓글: Sofia Santos 2020년 5월 6일
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
data=dir(fullfile(paste,'*.txt'));
for k=1:numel(data)
signal=data(k).name
data_org = importdata(signal,'\t',7); %text file with 7 columms
EDA=data_org.data;
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=N/fs;
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);
amp=abs(maximo-minimo);
disp('amplitude='); disp(amp); %amplitude da SCR
%% Export to excel
T = table(amp,'RowNames',{signals});
fileName='signals_EDA.xlsx';
writetable(T,fileName);
end

채택된 답변

Guillaume
Guillaume 2020년 5월 6일
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
data=dir(fullfile(paste,'*.txt'));
%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;
end
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
data=dir(fullfile(paste,'*.txt'));
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);
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.
  댓글 수: 1
Sofia Santos
Sofia Santos 2020년 5월 6일
Solved my problem :)
Thank you so much for your help and for such a detailed explanation!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by