Search number in one cell of a csv file

1 view (last 30 days)
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?

Accepted Answer

StefBu
StefBu on 21 Jan 2019
Edited: StefBu on 21 Jan 2019
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));

More Answers (1)

Stefan Langlouis
Stefan Langlouis on 21 Jan 2019
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 Comment
StefBu
StefBu on 22 Jan 2019
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

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!