Search number in one cell of a csv file

1 vue (au cours des 30 derniers jours)
Stefan Langlouis
Stefan Langlouis le 21 Jan 2019
Commenté : StefBu le 22 Jan 2019
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?

Réponse acceptée

StefBu
StefBu le 21 Jan 2019
Modifié(e) : StefBu le 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));

Plus de réponses (1)

Stefan Langlouis
Stefan Langlouis le 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 commentaire
StefBu
StefBu le 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

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by