So if one was to want to use Google protocol buffers in Matlab and using a Windows computer what would be the best way to do that since Matlab is not in the list of supported languages?
Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.
The proto-backwards-compatibility plugin is a Maven plugin to run a backwards compatibility check on a set of protobuf IDL files. The plugin can be integrated into various phases of a maven build to check that any changes to a set of . proto files are backwards compatible.
Nested Class SummaryA Printer converts protobuf message to JSON format. A TypeRegistry is used to resolve Any messages in the JSON conversion.
I haven't seen an answer on this and I thought the solution was a bit obscure so I am going to post a how to for matlab_out using the protoc.exe
A how to for google protocol buffer matlab out, this is using resources from the internet I will also include a zip file containing all this already done.
Unzip protobuf-‘version#’.zip (looks like: protobuf-#.#.#)
Open file protobuf-#.#.# -> src
a. Under “nobase_include_HEADERS =
”
and below “$(GZHEADERS)
”
add the line “farsounder/protobuf/compiler/matlab/matlab_generator.h \
” (Note the back slash)
b. Under “libprotoc_la_LIBADD = $(PTHREAD_LIBS) libprotobuf.la
”
add lines
“farsounder/protobuf/compiler/matlab/matlab_generator.cc \
”
“farsounder/protobuf/compiler/matlab/matlab_plugin.cc \
”
c. Save the file and exit out
a. Under “#include <google/protobuf/compiler/java/java_generator.h>
”
add the line “#include <farsounder/protobuf/compiler/matlab/matlab_generator.h>
”
b. In main function add the lines
“// Proto2 Matlab
farsounder::protobuf::compiler::matlab::MatlabGenerator matlab_generator;
cli.RegisterGenerator("--matlab_out", &matlab_generator,
"Generate Matlab M files.");
”
c. Save the file and exit out
Help to find protobuf-#.#.# and the protobuf-matlab:
Follow these links:
https://code.google.com/p/protobuf-matlab/source/browse/
download the zip file
https://github.com/google/protobuf/releases
download the source code
Extra help, for using protoc.exe
protoc –matlab_out=./ -I./ afunprotofile.proto
”The FarSounder code was nice, but it is quite old and unmaintained. The easiest way to produce Matlab compatible code is to just use the Java version of Protobuf. This solution should work on any platform that supports Matlab and Java.
protoc
compiler and output Java sourceedit('classpath.txt')
I include these in a single JAR file output of the Protobuf and the two runtime libraries.
I wrote a simple Java wrapper class to hide the MyProtobuf.Builder
return type from Matlab that I added to the JAR file
public class MyProtobufWrapperWrapper {
public static MyProtobuf.Builder newBuilder()
{
return MyProtobuf.newBuilder();
}
}
In Matlab
p = com.cameronpalmer.MyProtobufWrapper.newBuilder();
p.setIdentifier(java.util.UUID.randomUUID().toString());
p.setTimestampMilliseconds(timestamp);
p.setAngleRadians(0);
p.addAllChannelSamples(channel_vector);
planeWaveBuilt = p.build();
byteArray = planeWaveBuilt.toByteArray();
As Cameron Lowell Palmer's answer suggests, the way to go is via Java.
Is I lost a couple of hours today on this problem, I would like to elaborate some more. I started with Cameron's answer but I had to do a couple of more steps. Essentially, I had to do all of the following:
protoc --java_out=./ your_file.proto
sudo apt install libprotobuf-java
/usr/share/java/protobuf-3.6.1.jar
; its path will be used later on. The name should always follow the pattern protobuf-version.jar
or protobuf-java-version.jar
, therefore locate protobuf- | grep jar$
should reduce the search space for you../x/y/z/MyProto.java
). Use your package path instead of x/y/z. If you did not declare java package explicitly in the proto file, then protoc just used your filename as package name. Either way, you can probably check where the protoc generated files went by yourself. Contents of MyProto.java are listed below. Just replace YourProtoFileName
and YourMessageName
with your stuff. Note that this step is not optional, as this will not generate a simple helper class. For the life of me, Matlab would not let me use inner classes directly (in java, YourMessageName
is the inner class of YourProtoFileName
). But with the above helper, it was quite happy to generate them for me. Note that if you have more than one message defined in your proto file, you might need to expose more than one builder in this way. And if you only need to read protobuffers, then you might need to export just YourMessageName
and not Builder
.package x.y.z;
import x.y.z.YourProtoFileName; // if you do not know it, do `ls x.y.z/*.java`
public class MyProto {
public static YourProtoFileName.YourMessageName.Builder newBuilder() {
return YourProtoFileName.YourMessageName.newBuilder();
}
}
version -java
. In my case it was 1.8, while the default java installed on my system (java -version
) is 11. I had to manually select java 1.8 for the next step, otherwise the whole thing did not work. Even worse, Matlab only produced a very nondescript error "No class x.y.z.YourProtoFileName." Thanks Matlab! You might need to install proper version first (sudo apt install openjdk-8-jdk
) and then use update-java-alternatives
or just locate javac
for the appropriate java version on your system./usr/lib/jvm/java-8-openjdk-amd64/bin/javac x.y.z/*.java -cp /usr/share/java/protobuf-3.6.1.jar
. This will generate class files in ./x/y/z/
.jar cvf ./YourProtoFilename.jar x/y/z/*.class
. Note that this command line works for me, since I've put all classes, including MyProto, in the same package. You might need to adapt it to your needs.% make Matlab aware of your new classes
javaaddpath('./')
% tell Matlab where protobuf dependancy lives (use the path from step 3)
javaaddpath('/usr/share/java/protobuf-3.6.1.jar')
% test if the classes were found
methods('x.y.z.YourProtoFilename.YourMessageName')
% if methods are listed then you are good to go
% use the helper form step 4
b = x.y.z.MyProto.newBuilder();
% now you have a builder you can use to build your protobuf message
The same procedure works on octave too. With a bit different syntax for java inside octave. And octave was less picky about the java version in my case. YMMV
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With