MATLAB: Count Peaks, csv-files

csvdata importfindpeaksMATLAB

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:
  1. 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?
  1. 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

Best Answer

Roger mentioned addpath to solve the subdirectory problem. To read a series of files, see the FAQ
I recommend the dir solution almost always. In short for your case...
d=dir('2014*.csv'); % get the list of .csv files beginning w/ 2014

for i=1:length(d) % loop over the list
[dat,txt]=xlsread(d(i).name); % read the ith file
% do whatever with the data here...
...
end
For a single file I used one of your links to get here...
>> d=dir('2014*.csv'); % get the list of .csv files beginning w/ 2014
>> [dat,txt]=xlsread(d.name);
>> whos dat
Name Size Bytes Class Attributes
dat 25004x2 400064 double
>> whos txt
Name Size Bytes Class Attributes
txt 2x2 276 cell
>> txt
txt =
'Zeit' 'Kanal A'
'(us)' '(V)'
>> dat(1:3,:)
ans =
-3.9008 0.1575
-3.8968 0.1969
-3.8928 0.1575
>>
Your problems on data types will go away when you read the data as above and use the numeric data array dat (or whatever you choose as a variable name instead of dat, of course).
And, if you know the headings and don't care, just return the numeric data...
dat=xlsread(d(i).name); % read, return numeric only