Read csv in multiple directories in a loop

1 次查看(过去 30 天)
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 个评论
Jan
Jan 2022-4-1
By the way. you can replace
rawdataFileIdx = cellfun(@(name) strcmpi(name,'rawdata.csv'),{files.name});
by
rawdataFileIdx = strcmpi({files.name}, 'rawdata.csv'));

请先登录,再进行评论。

采纳的回答

Jan
Jan 2022-4-1
编辑:Jan 2022-4-1
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 个评论
Alex Perrakis
Alex Perrakis 2022-4-1
and within the last file there is a single measurement, so i need it to go back and forth

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by