Search number in one cell of a csv file

5 visualizaciones (últimos 30 días)
Stefan Langlouis
Stefan Langlouis el 21 de En. de 2019
Comentada: StefBu el 22 de En. de 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?

Respuesta aceptada

StefBu
StefBu el 21 de En. de 2019
Editada: StefBu el 21 de En. de 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));

Más respuestas (1)

Stefan Langlouis
Stefan Langlouis el 21 de En. de 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 comentario
StefBu
StefBu el 22 de En. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by