How to run clojure from matlab - matlab

How can I run a clojure script from matlab?
I tried following:
run matlab with jdk 1.7 and then call java
MATLAB_JAVA=/usr/lib/jvm/java-7-oracle/jre matlab
in the matlab, set classpath and use clojure compiler
javaaddpath([pwd '/lib/clojure-1.5.1.jar'])
import clojure.lang.RT
Here I got error:
Error using import
Import argument 'clojure.lang.RT' cannot be found or cannot be imported.
When I writing java class that runs clojure, everything working from console, but whould not run from matlab.
please advice.

It looks like this is a problem with Clojure not being happy running from Matlab's "dynamic classpath". I got the same error with Matlab R2014a on OS X 10.9, using either the bundled JVM or Java 1.7.0u51. But if I add clojure-1.5.1.jar to the static classpath by putting it in a custom javaclasspath.txt in the Matlab startup directory, then the Clojure classes become visible.
>> version -java
ans =
Java 1.7.0_51-b13 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
>> cloj = clojure.lang.RT
cloj =
clojure.lang.RT#77de6590
Hacking the Java classpath
You use the "class path hacking" approach in this answer to add entries to the static classpath from the Matlab command line and not have to muck around with a custom Matlab setup. The answer there involves writing a new Java class, but you can do the equivalent in pure M-code.
function javaaddpathstatic(file)
%JAVAADDPATHSTATIC Add an entry to the static classpath at run time
%
% javaaddpathstatic(file)
%
% Adds the given file to the STATIC classpath. This is in contrast to the
% regular javaaddpath, which adds a file to the dynamic classpath.
%
% Files added to the path will not show up in the output of
% javaclasspath(), but they will still actually be on there, and classes
% from it will be picked up.
%
% Caveats:
% * This is a HACK and bound to be unsupported.
% * You need to call this before attempting to reference any class in it,
% or Matlab may "remember" that the symbols could not be resolved.
% * There is no way to remove the new path entry once it is added.
parms = javaArray('java.lang.Class', 1);
parms(1) = java.lang.Class.forName('java.net.URL');
loaderClass = java.lang.Class.forName('java.net.URLClassLoader');
addUrlMeth = loaderClass.getDeclaredMethod('addURL', parms);
addUrlMeth.setAccessible(1);
sysClassLoader = java.lang.ClassLoader.getSystemClassLoader();
argArray = javaArray('java.lang.Object', 1);
jFile = java.io.File(file);
argArray(1) = jFile.toURI().toURL();
addUrlMeth.invoke(sysClassLoader, argArray);
So, use this javaaddpathstatic() instead of javaaddpath() and your code might work.

Related

Version mismatch leading to `exportgraphics` error

When running this code in Matlab 2022a (on Windows)
fig = figure;
exportgraphics(fig, 'd:\plot.emf');
I get the error
Error using exportgraphics
The class matlab.graphics.internal.mlprintjob has no Constant property or Static method named 'containsUIElements'.
I used the same exportgraphics command above for a long time, without problems. In the meantime, I copied the exportgraphics.m file from c:\Program Files\MATLAB\R2022a\toolbox\matlab\graphics\printing\en\ to c:\Program Files\MATLAB\R2019b\toolbox\matlab\graphics\printing\, in the hopes of being able to use it in Matlab 2019b too. That didn`t work, and when I exited Matlab 2019b and deleted the .m file from its folder, then entered Matlab 2022a, the above error happened.
There's probably been a confusion of file versions, although there seems to now only ever be the 2022a version left:
>> which -all exportgraphics
C:\Program Files\MATLAB\R2022a\toolbox\matlab\graphics\printing\exportgraphics.p
Running restoredefaultpath and savepath doesn't help. What else can I try?
A second question would be: is there a way to make exportgraphics work in Matlab 2019b, if (clearly) the method of just copying the file over from v2022a does not work?

Set file-level option to scalapb project

I'm using ScalaPB (version 0.11.1) and plugin sbt-protoc (version 1.0.3) to try to compile an old project with ProtocolBuffers in Scala 2.12. Reading the documentation, I want to set the file property preserve_unknown_fields to false. But my question is, where? Where do I need to set this flag? On the .proto file?
I've also tried to include the flag as a package-scoped option by creating a package.proto file next to my other .proto file, with the following content (as it is specified here):
import "scalapb/scalapb.proto";
package eur.astrata.eu.bigdata.tpms.protobuf;
option (scalapb.options) = {
preserve_unknown_fields: false
};
But when trying to compile, I get the following error:
[libprotobuf WARNING T:\src\github\protobuf\src\google\protobuf\compiler\parser.cc:648] No syntax specified for the proto file: package.proto. Please use 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.)
scalapb/scalapb.proto: File not found.
package.proto:1:1: Import "scalapb/scalapb.proto" was not found or had errors.
I've also tried with syntax = "proto3"; at the beginning but it doesn't work.
Any help would be greatly appreciated.
From the docs:
If you are using sbt-protoc and importing protos like
scalapb/scalapb.proto, or common protocol buffers like
google/protobuf/wrappers.proto:
Add the following to your build.sbt:
libraryDependencies += "com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion % "protobuf"
This tells sbt-protoc to extract protos from this jar (and all its dependencies,
which includes Google's common protos), and make them available in the
include path that is passed to protoc.
It is important to add that by setting preserve_unknown_fields to false you are turning off a protobuf feature that could prevent data loss when different parts of a distributed system are not running the same version of the schema.

Training weka models in matlab: how to evaluate? Mssing some model type?

despite using weka I have models like libSVM and RseslibKnn, when I call them in Matlab it can't find them
enter image description here
and I noticed that actually they are not part of the models of the directories "classifiers".
enter image description here
I know how to crossvalidate a generic model but I can't use the simple validation with determinated training and test set. Here the code I'm using:
% memorize current folder and change folder
base_folder = cd;
cd 'C:\Program Files'
% adding weka in the java folder
javaaddpath("weka-3-8-4/weka.jar");
% loading arff training set
l = javaObject("weka.core.converters.ArffLoader");
l.setFile(javaObject("java.io.File","weka-3-8-4/data/weka_BEST_train_ESS.arff"));
tr = l.getDataSet;
tr.toString;
% building classfiers
c = javaObject("weka.classifiers.functions.MultilayerPerceptron");
tr.setClassIndex(tr.numAttributes - 1);
c.buildClassifier(tr);
c.toString;
% loading arff test set
l = javaObject("weka.core.converters.ArffLoader");
l.setFile(javaObject("java.io.File","weka-3-8-4/data/weka_BEST_test_ESS.arff"));
ts = l.getDataSet;
ts.toString;
% validation
e = javaObject("weka.classifiers.Evaluation", tr);
e.evaluateModel(c, ts); %?????????????????????????????????????????????????????????????????????
e.toSummaryString;
% going back to the current folder
cd (base_folder)
Answering to the first question:
libSVM and RseslibKnn are models available in separate Weka packages that need to be additionally installed in Weka. To install the packages run Weka package manager (menu Tools -> Package Manager in Weka GUI Chooser) and install LibSVM and Rseslib packages. The packages with their java jars are installed in the wekafiles directory. On Windows it is C:\Users\ <username>\wekafiles\packages by default.
I don't know Matlab but I guess that to use models from extra Weka packages you need to add also the package jar files to Matlab javapath after installing the packages in Weka:
javaaddpath("C:\Users\ <username>\wekafiles\packages\Rseslib\rseslib.jar");
javaaddpath("C:\Users\ <username>\wekafiles\packages\LibSVM\LibSVM.jar");
Alternatively, you can download the libraries directly from the web sites of the package providers, unpack them and add jars to Matlab javapath:
http://rseslib.mimuw.edu.pl/download.html
https://www.csie.ntu.edu.tw/~cjlin/libsvm/

Matlab missing dependency MEX-file

I have a script in matlab that calls other libraries. I use matlab version 2012a on linux . I get below error and I don't know how to fix it.
The error is :
Invalid MEX-file '/home/XXX/nearest_neighbors.mexa64':
libflann.so.1.8: cannot open shared object file: No such file or
directory
Error in flann_search (line 82)
[indices,dists] = nearest_neighbors('find_nn', data, testset, n, params);
Error in MyScript (line 73)
[nresult, ndists] = flann_search(Ntraindata', Ntraindata', resu.KNN, struct('algorithm','composite',...
That library you are referring to - https://github.com/mariusmuja/flann/ - has the nearest_neighbors function written in MEX code. MEX code is C code that is used to interface with MATLAB. People usually write heavily computationally burdening code over in MEX as it is known to handle loops and other things faster. The inputs come from MATLAB and are sent to this MEX function, and the outputs come from the MEX function and are piped back to MATLAB. It's basically a nice black box where you can use it just like any other MATLAB function. In fact, a lot of the functions that come shipped with MATLAB have MEX wrappers written to promote acceleration.
You are getting that error because you need to compile the nearest_neighbors function so that there is a MEX wrapper that can be called in MATLAB. That wrapper is missing because you haven't compiled the code.
First, you need to set up MEX. Make sure you have a compiler that is compatible with your version of MATLAB. You can do that by visiting this link:
http://www.mathworks.com/support/compilers/R20xxy/index.html
xx is the version number that belongs to your MATLAB and y is the character code that comes after it. For example, if you are using R2013a, you would visit:
http://www.mathworks.com/support/compilers/R2013a/index.html
Once you're there, go to your Operating System and ensure you have one of those supported compilers installed. Once you have that installed, go into MATLAB, and in the command prompt, type in:
mex -setup
This will allow you to set up MEX and choose the compiler you want. Given your error, you're using Linux 64-bit, so it should be very easy for you to get GCC. Just make sure that you choose a version of GCC that is compatible with your version of MATLAB. Once you choose the compiler, you can compile the code by doing this in the command prompt:
>> mex -v -O nearest_neighbors.cpp
This should generate the nearest_neighbors MEX file for you. Once that's done, you can now run the code.
For more detailed instructions, check out FLANN's user manual - http://people.cs.ubc.ca/~mariusm/uploads/FLANN/flann_manual-1.8.4.pdf - It tells you how to build and compile it for MATLAB use.
Good luck!

Linking and LOADING static .lib with mex

So, I have a MEX gateway script file that calls my C source code. I've used the -L and -I commands to link my 64-bit compiled GSL libraries (.libs) to my mex executable, which is then compiled under the extension of .mexw64.
I want for this executable to be transferred to another windows machine and run fine, without any GSL libraries installed. That is the the only solution, I don't care what he arguments are regarding the benefits of the dynamic linking/code generation upon compile-time are. I want an executable that has every function not only (of course) pre-declared, but also PRE-DEFINED.
I was lead to believe that this is what 'static' linking is vs. dynamic; but I've read some contradictory definitions all around the interwebs. I need a completely 100% standalone, singular file.
Supposedly you can link the actual .obj file in the mex function, which I can generate, but unfortunately I then get unresolved symbol errors.
Someone else mentioned that I can use the -l (lowercase L) to directly link the actual .lib(s) needed, statically, but that is NOT true.
So is there anyone that can lead me in the right direction, either how to have everything not only linked but to also have the DEFINITIONS linked and ready to load when executable is run--completely standalone, or why I am running into unresolved symbols/linker errors when I include my .obj file? Am I misunderstanding something elementary about the linking process?
Also: To elaborate a bit more, I have the GSL libraries built and linked via Visual Studio for the 64 bit architecture, and I can link it easily with MATLAB, so that is not my problem (any more).
EDIT: I've seen the post here:
Generating standalone MEX file with GNU compilers, including libraries
This doesn't solve my problem, however, although it is the same question. I don't have access to gcc; it's finally compiling on the MSVS12 compiler in MATLAB, I'm not going try to recompile using GCC via MinGW (already tried, couldn't figure it out), so -static and .a options are out.
In your previous post, you mentioned that you decided to compile GSL library with Visual C++, using the VS solution provided by Brian Gladman.
Here is a step-by-step illustration on how to build a MEX-function that links against GSL libraries statically:
Download GNU GSL sources (GSL v1.16)
Download the matching Visual Studio project files (VS2012 for GSL v1.16)
Extract the GSL tarball, say to C:\gsl-1.16
Extract the VS project files on top of the sources, this will overwrite three files as well as add a folder C:\gsl-1.16\build.vc11.
Open Visual Studio 2012, and load the solution: C:\gsl-1.16\build.vc11\gsl.lib.sln
Change the configuration to the desired output: for me I chose platform=x64 and mode=Release
First you must build the gslhdrs project first
Now build the whole solution. This will create two static libraries cblas.lib and gsl.lib stored in C:\gsl-1.16\lib\x64\Release (along with corresponding PDB debugging symbols). It will also create a directory containing the final header files: C:\gsl-1.16\gsl
Next we proceed to build a MEX-function. Take the following simple program (computes some value from a Bessel function, and return it as output):
gsl_test.c
#include "mex.h"
#include <gsl/gsl_sf_bessel.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (nrhs != 0 || nlhs > 1) mexErrMsgTxt("Wrong number of args.");
plhs[0] = mxCreateDoubleScalar(gsl_sf_bessel_J0(5.0));
}
This is how to compile the above C code in MATLAB:
>> mex -largeArrayDims gsl_test.c -I"C:\gsl-1.16" -L"C:\gsl-1.16\lib\x64\Release" cblas.lib gsl.lib
Finally we test the MEX-file, and compare it against the value reported by MATLAB's own Bessel function:
>> x = gsl_test()
ans =
-0.1776
>> y = besselj(0,5)
y =
-0.1776
>> max(x-y) % this should be less than eps
ans =
8.3267e-17
Note that the built MEX-function has no external DLL dependencies (other than "Visual C Runtime" which is expected, and the usual MATLAB libraries). You can verify that by using Dependency Walker if you want. So you can simply deploy the gsl_test.mexw64 file alone (assuming the users already have the corresponding VC++ runtime installed on their machines).