How to make Google Protobuf work in Matlab?

So, if you want to use the Google protocol buffers in Matlab and use a Windows computer, what would be the best way to do this, since Matlab is not in the list of supported languages?

+5
source share
2 answers

The easiest way is to use Java

FarSounder code was nice, but it's pretty old and not supported. The easiest way to create Matlab-compatible code is to simply use the Java version of Protobuf. This solution should work on any platform that supports Matlab and Java.

Steps

  • Create a .proto File
  • Process file using protoc compiler and output Java source
  • Using IntelliJ or another tool creates a JAR file containing dependencies
  • Add JAR file to Matlab class path. edit('classpath.txt')
  • Restart matlab

Protobuf runtime dependencies

I include them in one output of the Protobuf JAR file and two runtime libraries.

  • Protobuf-java-3.3.0.jar
  • Protobuf-java-util-3.3.0.jar

Sample

I wrote a simple Java shell class to hide the return type MyProtobuf.Builder from Matlab, which 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 = planeWave.build(); byteArray = planeWaveBuilt.toByteArray(); 
+1
source

I did not see the answer to this, and I thought the solution was a bit unclear, so I'm going to post how to use protoc.exe for matlab_out

As for the google protocol matlab out protocol, 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 this: protobuf - #. #. #)

  • Open protobuf file - #. #. # β†’ src

  • Select your favorite editor (Notepad ++ is good) and modify Makefile.am (do not include quotation marks)

a. In the section " nobase_include_HEADERS = " and below " $(GZHEADERS) "

add the line " farsounder/protobuf/compiler/matlab/matlab_generator.h \ " (note the backslash)

b. In the section " libprotoc_la_LIBADD = $(PTHREAD_LIBS) libprotobuf.la "

add lines
" farsounder/protobuf/compiler/matlab/matlab_generator.cc \ "

" farsounder/protobuf/compiler/matlab/matlab_plugin.cc \ "

from. Save the file and exit it

  1. While still in the src directory, go to β†’ google β†’ protobuf-> compiler and change main.cc

a. In the section " #include <google/protobuf/compiler/java/java_generator.h> "

add the line " #include <farsounder/protobuf/compiler/matlab/matlab_generator.h> "

b. In the main function, add the lines " // Proto2 Matlab farsounder::protobuf::compiler::matlab::MatlabGenerator matlab_generator; cli.RegisterGenerator("--matlab_out", &matlab_generator, "Generate Matlab M files."); "

from. Save the file and exit it

  1. Unzip protobuf-matlab
  2. Open the protobuf-matlab β†’ src file and copy the farsounder directory to the protobuf - # file. #. # β†’ src
  3. Return to the protobuf - # file. #. # and in β†’ vsprojects and open protobuf.sln in Visual Studio, I believe that any version should work
  4. A popup window should appear that wants to convert the solution file to a newer version, and do it
  5. If you do not see explorer explorer open it using ctrl + alt + L
  6. Open a new Windows Explorer and go to protobuf - #. #. # β†’ src-> farsounder-> protobuf-> compiler-> matlab, now in the visual studio, using the solution explorer, expand libprotoc-> Header Files, now in Windows Explorer copy the matlab_generator.h file and paste it into the header directory.
  7. Still in the Solutions application, go to the Source Files directory and copy the matlab_generator.cc file
  8. Contract libprotoc and right click on protoc and click on properties
  9. In the section "Configuration Properties" β†’ "Binder" β†’ "General", "Edit" Enable incremental binding "No"
  10. In the section "Configuration Properties" β†’ "Linker-> Input", "Edit Additional Dependencies" a. Add the lines "Release \ libprotobuf.lib" and under "Release \ libprotoc.lib" b. Click OK in the Advanced Dependencies and OK windows on the duct properties page
  11. Change build type for release
  12. Right click on libprotobuf and select build, when done right click on libprotoc and select build
  13. When finished, right-click on protoc and select the assembly, it should provide you protoc.exe under protobuf - #. #. # β†’ vsprojects-> Release, now it will allow you to create matlab.m files by using the matlab_out command

Help find protobuf - #. #. # and protobuf-matlab:

Follow these links:

https://code.google.com/p/protobuf-matlab/source/browse/
download zip file

https://github.com/google/protobuf/releases

download source code

Additional help to use protoc.exe

  • Copy and paste the created protoc.exe file to where your .proto file is
  • Run cmd and change the directory where the protoc.exe and .proto files are located.
  • Run this command (let's pretend my .proto file name is "afunprotofile") " protoc –matlab_out=./ -I./ afunprotofile.proto "
+2
source

All Articles