Search number in one cell of a csv file
1 次查看(过去 30 天)
显示 更早的评论
Hi everybody,
I'm reading the header of an csv-file and want to get the numbers to a new variable for my fft.
My problem is, that the header is in one cell. ->See the header_csv.png
At the moment i read the whole header with this code i've found online:
fid = fopen('acq0003.csv', 'r');
header = textscan(fid, '%[^,],%[^,],%[^,\r\n]', 3);
data = transpose(fscanf(fid, '%g,%g,%g\n', [2, Inf]));
fclose(fid);
%Note that the data read by fscanf need to be transposed (I emphasized this by writing transpose instead of ').
for i = 1 : 3; disp(['#' cell2mat(header{i})]); end;
disp(data);
that works. But now I want to scan for the text "samples" and the "sample-rate" to get the following numbers as values for my fft.
Any idea how i can scan for these particular values?
0 个评论
采纳的回答
StefBu
2019-1-21
编辑:StefBu
2019-1-21
Try this code. It should work for you. If not feel free to ask.
Greetings
Stefan
% Get data from cell
head = header{1,1}{1,1};
% Define strings for search
NameStart = '#Sample rate: ';
LengthNameStart = length(NameStart);
NameEnd = 'Hz';
LengthNameEnd = length(NameEnd) - 1; %
% Determine indexes for string exctraction
StringStart = strfind(head, NameStart) + LengthNameStart;
StringEnd = strfind(head, NameEnd) - LengthNameEnd;
% Exctract string and convert to double
SampleRate = str2double(head(StringStart:StringEnd));
0 个评论
更多回答(1 个)
Stefan Langlouis
2019-1-21
1 个评论
StefBu
2019-1-22
Nearly correct. ;) You have to use this:
NameEnd = sprintf('\n');
You also have to search for the first new Line after your NameStart-index because stringfind will return all new Lines inside the header.
Greetings
Stefan
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!