Search number in one cell of a csv file

5 次查看(过去 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?

采纳的回答

StefBu
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));

更多回答(1 个)

Stefan Langlouis
Stefan Langlouis 2019-1-21
Yep it's working :) Thank you very much!
But just for the sake of understanding, I call the expression "/ n" for "NameEnd" if I did not have any text ending like Hz? Or how do i finisch the string then
  1 个评论
StefBu
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 CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by