svmtrain error "Group must be a vector." - matlab

I am trying to train a SVM with the Matlab interface of LibSVM, but I get following error
model = svmtrain(Classes(train),Attributes(train,:),'-s 0 -t 2');
??? Error using ==> svmtrain at 172 Group must be a vector.
See also comment here: support vector machines in matlab

The problem is, that Matlab is using its own SVM implementation which takes among other things the variables in different order.
To make it work, make sure that the LibSVM library is part of your Matlab's Search Path. One option would be to use the Matlab filebrowser (Current Folder) to go the LibSVM folder and use the menu Add to Path -> Selected Folders and Subfolders. If this solves your problem and you need LibSVM regularly, add the folder to your startup.m using addpath.
See also: Error: Undefined function or method 'svmtrain' for input arguments of type 'double'

Related

How to tell if a function is built-in or self-defined by its name?

I generate a call graph of a complex MATLAB system, and I want to know which functions are built-in and mark them.
Whether a function is built-in or not is most easily seen by the which command. For a given function name it displays the full path to the file that defines the function. For example, on my machine I see
>> which eig
built-in (/Applications/MATLAB_R2018b.app/toolbox/matlab/matfun/eig)
>> which solve
/Users/robert/Documents/MATLAB/cvx/lib/#cvxprob/solve.m % cvxprob method
>> which nosuchfunctionhere
'nosuchfunctionhere' not found.
telling me that eig is a built-in function, and solve a function that is part of the package cvx, and that nosuchfunctionhere is defined nowhere.
MATLAB makes a distinction between "built-in functions" (i.e. no M-file or MEX-file exists, the code is built into the MATLAB executable) and other functions that are part of the MATLAB package but written as M-files or MEX-files.
As Robert showed, the which function will tell you if a function is "built-in" or not, and it will give you a path.
For example, eig is a built-in function (the path given is a file containing the documentation):
>> p = which('eig')
p =
'built-in (/Applications/MATLAB_R2017a.app/toolbox/matlab/matfun/#single/eig)'
imshow is not built-in, but part of the core MATLAB toolbox:
>> p=which('imshow')
p =
'/Applications/MATLAB_R2017a.app/toolbox/matlab/images/imshow.m'
imdilate is a function that comes with the Image Processing Toolbox:
>> p = which('imdilate')
p =
'/Applications/MATLAB_R2017a.app/toolbox/images/images/imdilate.m'
and prettyplot is a function I wrote myself:
>> p = which('prettyplot')
p =
'/Users/cris/matlab/toolbox/cris/prettyplot.m'
To distinguish between these 4 cases, first check to see if the returned string begins with "built-in", then check to see if it contains fullfile(matlabroot,'toolbox','matlab'), indicating it is part of the core MATLAB toolbox, then check to see if it contains fullfile(matlabroot,'toolbox'), indicating it is part of another official toolbox:
function_name = 'eig';
p = which(function_name);
if startsWith(p,'built-in')
disp('built-in')
elseif contains(p,fullfile(matlabroot,'toolbox','matlab'))
disp('part of core MATLAB toolbox')
elseif contains(p,fullfile(matlabroot,'toolbox'))
disp('part of an official MATLAB toolbox')
else
disp('not an official MATLAB function')
end
However, do note that some functions could be overloaded! And if you're examining your source code to check which functions are being used, you need to know the types of the arguments passed. For example:
>> which -all eig
built-in (/Applications/MATLAB_R2017a.app/toolbox/matlab/matfun/#single/eig) % single method
built-in (/Applications/MATLAB_R2017a.app/toolbox/matlab/matfun/#double/eig) % double method
/Users/cris/newdip/target/dip/share/DIPimage/#dip_image/eig.m % dip_image method
Here you can see that there are three eig functions, one is used if its input argument is of type single, one if it is double, and one if it is dip_image (a custom class). Depending on the input, the function eig used is built-in or a 3rd party function.
The sad part is, you won't know which one is used until you run your code. You can manually check what values the input variables have, sometimes it is clear. But this is not always the case, the type might depend on data outside of the function you're examining.
So, the best way to collect a list of functions your program uses is to run the profiler.
Another alternative: the MATLAB Compiler (a separate product) will collect all source M-files your function uses, and package them together into a single distributable package.
Although I think solutions based on which are better, for completeness, we should also consider the function exist for this. From the documentation:
exist name returns the type of name as a number. This list describes the type associated with each value:
0 — name does not exist or cannot be found for other reasons. For example, if name exists in a restricted folder to which MATLAB® does not have access, exist returns 0.
1 — name is a variable in the workspace.
2 — name is a file with extension .m, .mlx, or .mlapp, or name is the name of a file with a non-registered file extension (.mat, .fig, .txt).
3 — name is a MEX-file on your MATLAB search path.
4 — name is a loaded Simulink® model or a Simulink model or library file on your MATLAB search path.
5 — name is a built-in MATLAB function. This does not include classes.
6 — name is a P-code file on your MATLAB search path.
7 — name is a folder.
8 — name is a class. (exist returns 0 for Java classes if you start MATLAB with the -nojvm option.)
So when we try this on the examples shown earlier:
>> exist eig
ans =
5
>> exist solve
ans =
2
>> exist nosuchfunction
ans =
0
Just type open followed by the function name in command window
open function_name
And function_name will be displayed into editor, you might see Mathwork copyright inside it if it's a build in function otherwise it's not
This is how the copyright looks
% Copyright 1993-2016 The MathWorks, Inc.

Matlab Error: Undefined function 'knnclassify' for input arguments of type 'double'

I am trying to do knnclassify on test_data(10000X784), train_data(50000X784), train_label(50000X1) and k = 1
And I am calling this function as follows:
label = knnclassify(test_data,train_data,train_label,k);
Background:
Where train_label is numeric equivalent digit of the data given in train_data. I want to classify my test_data. The data in both the train and test are in random order, but the train_label totally corresponds with the data in the train_data.
In my friend's workstation it works fine, but in my laptop it gives this error:
Undefined function 'knnclassify' for input arguments of type 'double'.
What could be the reason for the issue and how to solve it? Do I need to install any package? If yes how?
This error means that the function cannot be found in matlab, so you might missing the required toolbox. Just as #schorsch said you need to install the Bioinformatics toolbox.
A way to find out if the function is available in Matlab is typing which knnclassify. The output will be the route where the function resides or 'knnclassify' not found. otherwise.

MATLAB: Undefined function 'libsvmwrite' for input arguments of type 'char'

I'm trying to convert a CSV file to LIBSVM/SVMlight format. I found the following code to do it:
SPECTF = csvread('SPECTF.train'); % read a csv file
labels = SPECTF(:, 1); % labels from the 1st column
features = SPECTF(:, 2:end);
features_sparse = sparse(features); % features must be in a sparse matrix
libsvmwrite('SPECTFlibsvm.train', labels, features_sparse);
I used it on Octave on a specific file, and it worked properly.
However, when trying it on MATLAB, I received the error:
Undefined function 'libsvmwrite' for input arguments of type 'char'.
Neither "labels" nor "features_sparse" are chars... (they are doubles). Where is my error? Thanks!
The first argument for libsvmwrite is of type char ( the filename 'SPECTFlibsvm.train'). I think the problem is on the error message given by Matlab. The error message says that the function doesn't exist for input of type char, but most likely it should be that it doesn't exist at all (the message could maybe make sense if Matlab was designed as an OPP language).
Anyway, you simply don't have the libsvmwrite function in your path, or you somehow messed up the installation of the libsvm interface.
Probably you loaded only the source code, which is written in c. You need to compile it. Go to the matlab directory of libsvm and read the instructions.

kmeans example in matlab does not run

It is so strange that when I copy and paste the following matlab example
http://www.mathworks.co.jp/help/toolbox/stats/kmeans.html
to the work place and it says:
??? Error using ==> kmeans
Too many input arguments.
Anybody has the same problem?
#carlodsc is right.
Doing which kmeans, I can see that my kmeans function is located there:
/usr/local/MATLAB/R2011a/toolbox/stats/stats/kmeans.m
Since yours is located somewhere else, it means that you have another kmean function that is executed.
You should remove it from the path by doing:
rmpath ..\MATLAB\PT-MT\NetLab
Ok Oli.
Another or perhaps better solution is increasing the priority of
the path '...MATLAB/R2011a/toolbox/stats/stats' proportional to other paths containing kmeans. this can be done easily in the 'set path' window.

how can this function "resizeColumnscore" resizes image?

I want to know how can this function(from MATLAB) resize the columns of an input image using weights an indices previously computed.
Which equations uses to do that?
resizeColumnsCore(double(in), weights', indices');
When I looked for a function called resizeColumnsCore in MATLAB 7.11.0 (R2010b) I didn't find anything. However, I did find a MEX-file by that name in MATLAB 7.8.0 (R2009a) in this subdirectory of the Image Processing Toolbox:
C:\Program Files\MATLAB\R2009a\toolbox\images\images\private\
I guess they've phased it out or replaced it with another function in newer MATLAB versions. Now, if you want to know what the MEX-file does, you need to look at the source code it is compiled from. Luckily, it appears that this source code resizeColumnsCore.cpp can be found in the following directory:
C:\Program Files\MATLAB\R2009a\toolbox\images\images\private\src\misc\
And you can look through that code to determine the algorithms used to resize the columns of an image given a set of weights and indices.
Now, if you want to know how these input arguments to resizeColumnsCore are computed, you'll have to look at the code of a function that calls it. I know of at least one function in the IPT that calls this function: IMRESIZE. If you type edit imresize at the command prompt it will open that function in the Editor, allowing you to look through the code so you can see how the arguments to resizeColumnsCore are created.
What I can tell you for R2009a is that there is a subfunction in the file imresize.m called contributions which computes the weights and indices that are ultimately passed as arguments to resizeColumnsCore. That is where you will want to start looking to determine what algorithms are used to compute these arguments.
Looks like this isn't a proprietary MATLAB function. Could we see some code or a link to the code?