Smoothing for multiple csv files

5 views (last 30 days)
Hi! I smoothed some data for an experiment for one partipants, now I want to do that for multiple participants and save this to a table. Can you sopport me with this? Thanks. The code for one participant looks like this:
% Set the filename
filename = 'P01.xlsx';
% Import the data from the Xlsx file
data = xlsread(filename);
% assess the data
y = (data(:,1));
x = (data(:,5));
% Now I will smooth the data after the minimal pupil size
smoothdata = round(smooth(x(380:2000),y(380:2000),0.1,'loess'))
% Combine the smoothed data with the real data
combinedData = [y(1:379);smoothdata]
  1 Comment
Dyuman Joshi
Dyuman Joshi on 20 Feb 2023
You can run the code through a for loop and save the data in a table according to the number of participants.

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 6 Mar 2023
Edited: Mathieu NOE on 6 Mar 2023
hello
xlsread is a bit outdated , you could try more recent alternatives (readtable, readcell , readmatrix,...)
suggestion below (a simple for loop)
you can improve the sorting process by using this Fex submission (the native dir function is not perfect for that)
the result is stored as a cell array (combinedData{k})
but you can also use vertical or horizontal concatenation if the data has always same size
fileDir = pwd; % current directory (or specify which one is the working directory)
S = dir(fullfile(fileDir,'P*.xlsx')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order , see :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
for k = 1:length(S)
filename = S(k).name % to actually show filenames are sorted (see command window)
% Import the data from the Xlsx file
data = readmatrix(fullfile(fileDir, filename));
% assess the data
y = (data(:,1));
x = (data(:,5));
% Now I will smooth the data after the minimal pupil size
smoothdata = round(smooth(x(380:2000),y(380:2000),0.1,'loess'))
% Combine the smoothed data with the real data
combinedData{k} = [y(1:379);smoothdata]
end

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!