Count Peaks, csv-files
Mostra commenti meno recenti
Hey there! I'm a clueless beginner when it comes to Matlab. So if you would be so kind and answer my questions as simple as possible. I have many problems with what I want to do in Matlab:
- Item one Import csv-files in a loop. My idea:
for k=1:NDatei
if ~exist(sprintf('20140512-0013_%03d.csv',k), 'file')
errordlg(['File ' num2str(k) ' does not exit'],'File Error');
break ;
end
data=importdata(sprintf('20140512-0013_%03d.csv',k)) ;
end
Whereas my data has names like '20140512-0013_001.csv', '20140512-0013_002.csv',.. Problem is that Matlab tells me that my files don't exist. I think it's a problem with the path. Do you know why Matlab doesn't find the files although they are in a subfolder of the Matlab folder?
- Item two Findpeaks: I want to count the Peaks in each file with a certain threshold and minimal peak distance. Apparently Matlab has some problems with my imported files, because I get the Error
Error using findpeaks
Expected X to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64
Instead its type was char.
Error in findpeaks>parse_inputs (line 90)
validateattributes(Xin,{'numeric'},{'nonempty','real','vector'},...
Error in findpeaks (line 71)
[X,Ph,Pd,Th,Np,Str,infIdx] = parse_inputs(Xin,varargin{:});
Thank you so much for your help. I will attach the files as an example. I'm really looking forward to your answers and thoughts. Cheerio! Tanja
Risposta accettata
Più risposte (2)
dpb
il 28 Mag 2014
...how I can combine the threshold and the minpeakdistance in find peaks?
They're named-value pairs, along w/ the rest of the options--just string them out as needed...
[pks,locs]=findpeaks(data,'minpeakdistance',3, 'threshold', pi, 'npeaks', 42);
etc., ...
Roger Wohlwend
il 26 Mag 2014
Modificato: Roger Wohlwend
il 26 Mag 2014
You probably cannot open the files because the folder is not on the MATLAB search path. In your code add the following line at the beginning of the skript or function:
addpath(Path_of_the_folder_that_contains_the_csvFiles)
If you do that, Matlab will find the files and open them.
Concerning the csv files: Matlab has a problem with the first two lines that contain text. That is why it is not possible to use the function csvread. You can, however, use the function xlsread.
[~,~,raw] = xlsread('20140512-0013_001.csv')
Now delete the first three rows because they contain no numerical data:
raw = raw(4:end)
Extracting the data is, however, not so simple:
T = length(raw);
Data = NaN(T,2);
for k = 1 : T
C = strsplit(raw{k},',');
Data(k,1) = str2double(C{1});
Data(k,2) = str2double(C{2});
end
Now you have a matrix Data with the numerical data of the file. Use that matrix for any computations.
1 Commento
dpb
il 26 Mag 2014
You can, however, use the function xlsread.
[~,~,raw] = xlsread('20140512-0013_001.csv')
That's the hard way to use xlsread for the purpose, however..use the form
[dat,txt] = xlsread(filename);
instead. Then the numeric data will be in the array dat and the header text in txt automagically.
Or, use importdata that can transparently find the header lines.
Or, use textread with the 'headerlines' option--
dat=textread(filename,[],'headerlines','3);
Categorie
Scopri di più su Low-Level File I/O in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!