Read csv in multiple directories in a loop

1 Ansicht (letzte 30 Tage)
Alex Perrakis
Alex Perrakis am 1 Apr. 2022
Kommentiert: Alex Perrakis am 1 Apr. 2022
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 Kommentar
Jan
Jan am 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'));

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 1 Apr. 2022
Bearbeitet: Jan am 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 Kommentare
Alex Perrakis
Alex Perrakis am 1 Apr. 2022
and within the last file there is a single measurement, so i need it to go back and forth
Alex Perrakis
Alex Perrakis am 1 Apr. 2022
i got it thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by