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

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')

Related

Append legends. Whats wrong with my code?

I'm trying to add legends the plot when it adds a curve. I can't see whats wrong with my code, can someone please help? I'm using matlab r2015a on ubuntu.
x=1:5;
v=1:5;
plot(x,v)
[~,~,plots,str] = legend('1');
hold on
for i=4:10
pl=plot(x,v*i);
[~,~,plots,str]=legend([plots;pl],str,num2str(i))
end
when i run it i get:
plots =
1x2 Line array:
Line Line
str =
'1' '4'
Error using vertcat
Dimensions of matrices being
concatenated are not
consistent.
So its means it works the first lap but not the second.
As discussed in the comment, the way Matlab handles legends is different than in earlier releases; they are now legend objects.
In any case, to solve your problem, which is a dimension mismatch problem, you can simply concatenate vertically the outputs you get using the transpose operator, because Matlab returns a horizontal array of line and text objects whereas you want a vertical array. Therefore, using plots.' and pl.' works fine. Also, it's good practice not to use i as a loop counter since it represents the imaginary unit.
clear
clc
close all
x=1:5;
v=1:5;
plot(x,v)
[~,~,plots,str] = legend('1');
hold on
for k=4:10
pl=plot(x,v*k);
[LegendObject,~,plots,str]=legend([plots.';pl.'],str,num2str(k));
end
%// Use the legend object to modify its properties/location
set(LegendObject,'Location','NorthWest');
Output:

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!

Using getpts to get selected points

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'));

Plotting error values in MATLAB for highly dense temporal data

I know that we can plot error values in MATLAB using errorbar.m
However, I need to know if it possible to plot error values as a "faded tape" on the data so the actual data values are still visible. As you can see from the plot below generated using errorbar.m, the data line is overwhelmed by the error bars.
Use patch, with transparency to see the other data series underneath:
xdata = [...];
value =[...];
errors = [...];
patch_x = [xdata fliplr(xdata)];
patch_y = [(value + errors) fliplr(value - errors)];
figure;
hold on;
patch(patch_x,patch_y,'facealpha',0.5,'edgecolor','none');
plot(xdata,value)

Matlab cell to array not working

I have this cell (16x1) in MATLAB:
eventIDs =
'explosion'
'light'
'darkness'
'atomic'
...
..
now I want to use this :
%First bar plotting!
bar(duration_vector);
d = size(duration_vector);
labels = cell2mat(eventIDs);
xticklabel_rotate([1:d],45,eventIDs,'interpreter','none');
set(gca, 'XTick', 1:d, 'XTickLabel', labels);
I want to plot a bar graph but my events I too long, and I want them rotated to look good!
but when I run the code
I get this:
??? Error using ==> cat
CAT arguments dimensions are not consistent.
Error in ==> cell2mat at 85
m{n} = cat(1,c{:,n});
Error in ==> extract_data at 52
labels = cell2mat(eventIDs);
You don't need to do cell2mat. That tries to create a 2D matrix of characters (which fails because your strings are different lengths).
You also don't need the set(... line because xticklabel_rotate already sets the labels.
cell2mat in Matlab only works if your cell has a consistent number of columns in all rows. That is so because Matlab can't handle normal arrays with a variable number of columns per row, and that's generally the case of string matrices.
That said, cell manipulations is almost equal to matrix manipulation, the only difference being the indexation method: matrices use square brackets ([) and cells use curly brackets.
I googled the code of this function you're using, the xticklabel_rotate, and found the fileexchange link to the function here. There, the example given by the author uses a cell, instead of a matrix.
So I'm guessing that you can drop this cell2mat off because I thik you don't need to set the Xticks with the set function you're using. The xticklabel_rotate should do that.
I think you should try this:
%First bar plotting!
bar(duration_vector);
d = size(duration_vector);
xticklabel_rotate([1:d],45,eventIDs,'interpreter','none');