Using getpts to get selected points - matlab

I'm trying to use getpts to get the location of the selected points by the user.
I used it as follows:
[X,Y] = getpts(imread('xyz.jpg'));
But, got the following error:
Error using getpts (line 46)
First argument is not a valid handle.
Error in program (line 7)
[X,Y] = getpts(imread('xyz.jpg'));
Why is that?
Thanks.

getpts needs a handle to either a figure or an axes, not a matrix as given by imread.
The simple solution is to display the image, then input either gca or gcf to getpts. Or you can manage handles on your own, but I don't think you wan't to do that.
Or to put it on one line with imshow:
[X,Y] = getpts(get(imshow('xyz.jpg'),'Parent'));

Related

How to change the class names in the legend of `plotroc`?

I'm plotting the results of a multiclass classifier using plotroc. The documentation is a bit spotty. I'd like to know how to change the automatically generated legend to my class labels.
I have 17 classes. My initial call to plotroc(target, output) produces this figure
I tried to update the legend to include my class labels using legend(class_labels) where class_labels is a 17x1 cell array with the labels. Here's the result
As you can see multiple labels were assigned to the grey line rather than simply replacing the labels in the first figure.
As an alternative, the documentation also suggests using the syntax plotroc(targets1,outputs2,'name1',...) to generate multiple plots, I assume one for each class with different thresholds. So I tried both
plotroc(target, output, class_labels)
Which returned an error
Error using horzcat Dimensions of matrices being concatenated are not
consistent.
Error in plotroc>update_plot (line 318)
titleStr = [names{i} ' ROC'];
Error in plotroc (line 111)
plotData = update_plot(param,fig,plotData,update_args{:});
And
plotroc(target, output, 'AtLocation', 'IsA', ... 'SymbolOf')
Which also returned an error
Error using plotroc (line 106) Incorrect number of input arguments.
Has anyone had success using the plotroc(targets1,outputs2,'name1',...) syntax or with changing the legend?
I have not found a solution using the plotroc function directly, but the results from the roc function can be plotted using plot allowing for more customization via the standard Matlab plotting options.
function myplotroc(target, output, class_labels)
[tpr,fpr,~] = roc(targets,outputs)
figure();
hold on;
set(gca, 'LineStyleOrder', {'-', ':', '--', '-.'}); % different line styles
for ii=1:length(class_labels)
plot(fpr{ii}, tpr{ii})
end
legend(class_labels);
end
Add two lines like this (supposing there are 4 ROC curves for the legends of W, L, D, and R respectively):
axisdata = get(gca,'userdata');
legend(axisdata.lines,'W', 'L', 'D', 'R')

MATLAB: findpeaks function

I'm using MATLAB 2013a and trying to find peak points of my data. When I tried code example given in
Find Peaks with Minimum Separation
I am getting the following error:
Error using uddpvparse (line 122)
Invalid Parameter/Value pairs.
Error in findpeaks>parse_inputs (line 84)
hopts = uddpvparse('dspopts.findpeaks',varargin{:});
Error in findpeaks (line 59)
[X,Ph,Pd,Th,Np,Str,infIdx] = parse_inputs(X,varargin{:});
I tried simple x and y vectors and got the same error. What can be the problem?
I have the same problem as you (R2013a on OSX) with the example by the Mathworks. For some reason it seems we can't use findpeaks with the x-and y-data as input arguments, We need to call the function with the y data and use the [peaks,locations] output to get the peaks/plot them.
It looks like in R2014b they changed some stuff about findpeaks that does not work with older versions...like calling the function with not output argument in R2014b plots the data/peaks without any additional step...but it does not for earlier versions.
Anyhow here is a way to workaround the problem. Call findpeaks with a single input argument (y data that is, you can use property/value pairs as well) and use the indices (locations) to show the peaks:
clc
clear
load sunspot.dat
year = sunspot(:,1);
avSpots = sunspot(:,2);
[peaks, locations] = findpeaks(avSpots)
plot(year,avSpots)
hold on
scatter(year(locations),avSpots(locations),40,'filled')
hold off
Output:
It might be worthwhile to contact The Mathworks about this. Hope that helps!

The name 'Units' is not an accessible property for an instance of class 'lineseries' - in getframe

How to capture plot image into memory? I am trying to use getframe, but failing
>> plot(h)
>> myhandle=plot(h)
myhandle =
174.0044
>> myframe=getframe(myhandle)
Error using graph2d.lineseries/get
The name 'Units' is not an accessible property for an instance of class 'lineseries'.
Error in getframe>Local_getRectanglesOfInterest (line 138)
if ~strcmpi(get(h, 'Units'), 'Pixels')
Error in getframe (line 56)
[offsetRect, absoluteRect, figPos, figOuterPos] = ...
From the MATLAB doc found here: http://www.mathworks.com/help/matlab/ref/getframe.html
F = getframe(h) gets a frame from the figure or axes identified by handle h.
When I do handle=plot(......), then type get(handle), and look at the type, you will see it is indeed a line series object, not an axis or figure.
To do what you want, use getframe(gca) or getframe(gcf). gca/gcf = getCurrentAxes/Figure respectively.
To be honest though, unless you are doing a semi complicated GUI or something like that, getframe really doesn't usually need any input arguments.

MATLAB - updating plot in gui?

Getting started with matlab guide, hit a stumbling block. Got it as simple as I can as a toy gui to illustrate my problem. A gui (named asas) has a pushbutton and an axis. The callback of the button reads
axesHandle= findobj(gcf,'Tag','axes1');
x=rand(randi(10+20,1),4);
plot(axesHandle, x)
There's no other code written by me (guide wrote it).
The 1st time I push the button, everything is fine: the plot gets done. The 2nd time around, I get an error, from the console:
Error using plot
Vectors must be the same lengths.
Error in asas>pushbutton1_Callback (line 83)
plot(axesHandle, x)
Error in gui_mainfcn (line 96)
feval(varargin{:});
etc...
I want to plot the new data x, replacing the old one.
It looks like matlab is not replacing the data to plot, but somehow trying to append to the plot?
I have searched, but haven't found anything that applies.
The explanation is not straightforward - and certainly not if you are new with MATLAB and its handle graphics subsystem.
Your code as it is, line by line:
axesHandle= findobj(gcf,'Tag','axes1');
x=rand(randi(10+20,1),4);
plot(axesHandle, x);
The first line attempts to locate in the current figure (gcf, "get current figure") any child object with the property 'Tag' set to the string 'axes1'. I guess you are aware of this? The second line of course generates some random data to plot. The third line plots the data in x.
But after the plot-call the property 'Tag' is actually reset to '' (the empty string), which in turn makes findobj fail in any subsequent searches for the axes-handle. The variable axesHandle with therefore NOT contain an actual handle but instead the empty matrix []. This will make plot default to another mode an interpret the empty matrix as data for the x-axes (the first argument to plot). This expectedly results in the error you receive:
...
Error using plot Vectors must be the same lengths.
...
The solution by Dan in the comment above is a workaround, but there is good sense in telling plot where to plot - especially in GUIs.
You can instead add a fourth line:
set(axesHandle,'Tag','axes1');
This will set property 'Tag' back to 'axes1' and any subsequent clicks on the button should now also work. And you can add more than one axes-objects now. If that is what you want to.

MatLab Plotting Function Matrix Dimension Error

I have a simple function written
function[] = myfun(p,q)
fminbnd(#(x)myfun1(q,p,b),0,1)
where myfun1's output is from the function quad.
How do I plot myfun? I've tried fplot(#(x)myfun(1,x),0,1) but this gives me a matrix dimensions must agree error...
Your question does not contain enough information to identify exactly where the problem lies, but one issue is certainly that myfun does not return any output. What should fplot plot, if there is nothing returned by the function?
Try
function out = myfun(p,q)
%# you may want to define b here
out = fminbnd(#(x)myfun1(q,p,b),0,1);
If that doesn't fix the issue, turn on the debugging mode by typing dbstop if error at the command line. This will show where exactly the error occurs, and allow you to inspect the variables for correct shape.