Working with structures, I cannot make an "if" work inside a "for" loop and cannot make "find peaks" work either; Any suggestions?

I am trying to work on vectors inside the structure (arrays), but when I have an if statement inside the for loop, it doesn't do what I am expecting it to. This is to remove the unwanted (pen-up) data in a drawing task, i.e., the threshold is 0.1.
Plus, I need to extract the corners of a drawing task data. i.e., we have asked subjects to draw some patterns for us like cubes and now I have to detect the corners of each drawing in order to compare it to the original pattern that we have (although the pattern is so big and I have problems matching them with the drawings) and analyse the tremor when drawing. What I was doing was plotting the graphs for each vector (I have x and y coordinates) to get the whole drawing and then I wanted to compare it to the original template using imshowpair (do we have any other ways of doing it that seems more reasonable to you?). But findpeak does not work either as I just need the corners that act as local maximums (I used the threshold to get only 4 maximums for cubes, etc.). I understand the code for this part is incomplete and I get errors, too (Attempted to access x(188); index out of bounds because numel(x)=13.); I just stopped as it was not doing what I wanted.
I have the simple code:
files = dir('.../controls/*.txt');
for k = 1:length(files)
data{k} = load(fullfile('.../controls/', files(k).name), '-ascii');
if data{k}(:,6) < 0.1
data{k}(:,6) = 0;
end
data{k}(:,2) = data{k}(:,2) * 20.32;
data{k}(:,3) = data{k}(:,3) * 15.24;
% Rescaling the drawings to match the
% dimensions of the active area of
% the tablet, which were 20.32 x 15.24 cm
plot(data{1,k}(:,2));
[pksx{k},locsx{k}] = findpeaks(data{1,k}(:,2),'MinPeakHeight',11);
plot(data{1,k},data{1,k}(:,2),data{1,k}(locsx{k}),pksx{k},'or')
plot(data{1,k}(:,3));
[pksy{k},locsy{k}] = findpeaks(data{1,k}(:,3),'MinPeakHeight',11);
plot(data{1,k},data{1,k}(:,3),data{1,k}(locsy{k}),pksy{k},'og')
end

2 Comments

Think you'll need to supply some data and perhaps figures to illustrate what you're expecting to do here...nobody can do anything with your code as is.

Sign in to comment.

 Accepted Answer

Are you sure about
if data{k}(:,6) < 0.1
data{k}(:,6) = 0;
end
That tests to see if all entries in column 6 are less than 0.1, and if so sets them all to 0, but if even one is judged to not be strictly less than 0.1, none of them are set to 0.
Perhaps you want
idx = data{k}(:,6) < 0.1;
data{k}(idx,6) = 0;

9 Comments

Hello Walter and thank you so much for answering my question. Yes, as I explained, the "if" part was not doing anything at all. I corrected my code by adding what you suggested and it is working perfectly fine now. Walter, do you know how I can get the corners (peaks) by any chance? I need to basically save all the six corners as it is for a pentagon drawing task (there are six main peaks and valleys and I need to locate them on my data and compare the image to the template that I have).
I have not happened to use anything fancier than findpeaks()
Brilliant. I thought the same, but for some reason I just get the error:
Attempted to access x(188); index out of bounds because numel(x)=13.
Error in findpeaks (line 51)
maxAt(nmax) = x(k+1);
Error in Controls (line 44)
[pksx{k},locsx{k}] = findpeaks(data{1,k}(:,2),'MinPeakHeight',10);
Here is the modified code:
files = dir('.../controls/*.txt');
for k = 1:length(files)
data{k} = load(fullfile('.../controls/', files(k).name), '-ascii');
% Locating the pen-up data
idx = data{k}(:,6) < 0.1;
data{k}(idx,6) = 0;
data{k}(idx,2) = 0;
data{k}(idx,3) = 0;
% I thought I should remove the x and y coordinates as well to make things easier for findpeaks() part.
% Rescaling
data{k}(:,2) = data{k}(:,2) * 20.32;
data{k}(:,3) = data{k}(:,3) * 15.24;
plot(data{1,k}(:,2));
[pksx{k},locsx{k}] = findpeaks(data{1,k}(:,2),'MinPeakHeight',10);
plot(data{1,k},data{1,k}(:,2),data{1,k}(locsx{k}),pksx{k},'or')
plot(data{1,k}(:,3));
[pksy{k},locsy{k}] = findpeaks(data{1,k}(:,3),'MinPeakHeight',10);
plot(data{1,k},data{1,k}(:,3),data{1,k}(locsy{k}),pksy{k},'og')
end
Unfortunately I do not have the signal processing toolkit to have findpeaks(). But could I ask you to use
which -all findpeaks
to check to see whether you are accidentally getting some other findpeaks() ?
/Applications/MATLAB_R2014a.app/toolbox/signal/signal/@dspdata/findpeaks.m % dspdata method
/Applications/MATLAB_R2014a.app/toolbox/signal/signal/findpeaks.m % Shadowed
That path looks okay.
Does the problem happen with the sample file you included, or with a different file?
Yes, it happens with all the data files that I have...
Sorry, nothing is obvious to me. I would need the toolbox to test with.
Thank you very much for helping anyway...please let me know if you had any other suggestions about this.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products

Tags

Community Treasure Hunt

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

Start Hunting!