Read csv in multiple directories in a loop

조회 수: 8(최근 30일)
Alex Perrakis
Alex Perrakis 2022년 4월 1일
댓글: Alex Perrakis 2022년 4월 1일
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일
i got it thanks!

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

추가 답변(0개)

범주

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