Read csv in multiple directories in a loop
3 次查看(过去 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
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
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!