Count Peaks, csv-files
8 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
dpb
2014-5-26
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
11 个评论
dpb
2014-5-28
编辑:dpb
2014-5-29
...using R2014a for Mac
Being as that is a newer release, I'd check for any updates patches that your installation is owed and if the symptom for xlsread is still there (or there are no notes that there's any patches for it) I suggest it's a bug and should send the file and bug report to TMW at mathworks.com
ADDENDUM
I whiffed on the 'Mac' part previous go-'round -- from the doc for xlsread
...
'basic' Flag to request reading in BASIC mode, which is the default for
systems without Excel for Windows. In BASIC mode, xlsread:
*Only reads XLS or XLSX files.
So, it's documented behavior that .csv files aren't supported on non-Windows platforms. Too bad the error messages aren't OS-sensitive to include that piece of trivia...
更多回答(2 个)
dpb
2014-5-28
...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., ...
0 个评论
Roger Wohlwend
2014-5-26
编辑:Roger Wohlwend
2014-5-26
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 个评论
dpb
2014-5-26
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!