Read csv in multiple directories in a loop

5 views (last 30 days)
Hello People, i have written a function to read a specific type fo .csv measurement data, it's always called rawdata.csv , my problem is i would like to write a generalized loop so matlab can take this csv from multiple directories that are in the same file and store in the same matrix. I hope understand what i talking about. I attach the function and a data file.
function [data, frequencies] = ReadSweep(msFolder)
%Script to read Sweep measurements
%Assuming there's only one file in msFolder named "rawdata.csv"
%First line of the files are frequencies in Hz, seperated by ",,"
%Rest of the file are lines with data. Each line contains real and
%imaginary part of data corresponding to frequencies in first line
%Between each line is a certain time defined by the trigger frequency which
%is not saved unfortunately
if nargin == 0 %No folder chosen
msFolder = uigetdir('F:\Messungen');
end
if ~msFolder
error('No Folder specified')
end
files = dir(msFolder);
rawdataFileIdx = cellfun(@(name) strcmpi(name,'rawdata.csv'),{files.name});
if ~any(rawdataFileIdx)
error('no rawdata.csv-file in folder')
end
fid = fopen([msFolder '\' 'rawdata.csv']);
frequencies = fgetl(fid);
frequencies = sscanf(frequencies,'%f,,');
dataColumns = length(frequencies)*2; %*2 cause data contain real + imag
stringData = fscanf(fid,'%s');
stringData = strrep(stringData,',-1,',',"-1",');
stringData = strrep(stringData,',1,',',"1",');
stringData = strrep(stringData,',0,',',"0",');
stringData = strrep(stringData,'","',' ');
stringData = strrep(stringData,',"',' ');
stringData = strrep(stringData,'",',' ');
stringData = strrep(stringData,'"',' ');
stringData = strrep(stringData,',','.');
data = sscanf(stringData, '%f');
data = reshape(data,dataColumns,[]); data = data';
fclose(fid);
and the file. And so imagine i have this named file in a multiple files withing a directory. In the file there is only this csv.
  1 Comment
Jan
Jan on 1 Apr 2022
By the way. you can replace
rawdataFileIdx = cellfun(@(name) strcmpi(name,'rawdata.csv'),{files.name});
by
rawdataFileIdx = strcmpi({files.name}, 'rawdata.csv'));

Sign in to comment.

Accepted Answer

Jan
Jan on 1 Apr 2022
Edited: Jan on 1 Apr 2022
files = dir(fullfile(msFolder, '**', 'rawdata.csv')); % Search recursively
n = numel(files);
data = cell(1, n);
frequencies = cell(1, n);
for k = 1:n
fid = fopen(fullfile(files(k).folder, files(k).name));
s = fgetl(fid);
frequencies{k} = sscanf(s,'%f,,');
dataColumns = length(frequencies)*2; %*2 cause data contain real + imag
stringData = fscanf(fid,'%s');
stringData = strrep(stringData,',-1,',',"-1",');
stringData = strrep(stringData,',1,',',"1",');
stringData = strrep(stringData,',0,',',"0",');
stringData = strrep(stringData,'","',' ');
stringData = strrep(stringData,',"',' ');
stringData = strrep(stringData,'",',' ');
stringData = strrep(stringData,'"',' ');
stringData = strrep(stringData,',','.');
dataM = sscanf(stringData, '%f');
data{k} = reshape(dataM, dataColumns,[] ).';
fclose(fid);
end
  3 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands 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!

Translated by