How to call (or compile) C++ code from Matlab 2016 - c++

I'm new with Matlab-C combination and I'm very lost. Any help will be very thankful.
Essentially, what I want is to use a C++ code from a simple matlab script. I've created some variables' values in matlab in order to give them as inputs to the C++ code and then capture theirs output of that C++ code and loading them into matlab.
I have a main.cpp and a makefile into the same folder. (they work well)
I've created a simple test.m file to create the variables' values for the C++ code and then, recover their outputs.
What I've done is to run the makefile using the mac terminal by typing
make
in order to create an executable called "benchmark". Each time I need to change a tiny variable's value from my matlab script, I must go to the terminal again, type
./benchmark
and then goes again to the matlab and run the test.m file to update the inputs of the C++ code. As you see, this is not the optimal way to work it.
So I'm wondering how to pass values created in matlab and in the same script updating the ./benchmark executable.
Thanks in advance guys.

Related

Python Sub-Process Coverage

Situation:
I'm attempting to get coverage reports for a project that uses both C++ and Python. I'm using LCOV/GCOV for C++, and attempting to use Coverage.py for the python stuff. The only issue is, most of the python code that's being used is simply utility functions being called one function at a time. No initialization, no real life-cycle, or exit. So no real way to use the API to start/stop/save, or use the coverage command line to measure.
With this, I thought the easiest way to accomplish this would be using the sitecustomize.py method like outlined here. I have gotten that to work, and it measures all configured python code as expected. Now I'm looking at how to accomplish this with compiled python code (.pyc).
I can get it to work if I keep source(.py) and (.pyc) in the same directory when running, and then reporting. However, I'm looking for a way to RUN the files and generate the measurement data. Then at a later time point to the actual source files, and run the actual reports. Ideally I wouldn't need the source(.py) files at all, but I haven't found a way to accomplish this.
Objective:
In the end I want to be able to compile the python files(.pyc), install them on the target, and run coverage like stated above. It will generate coverage data files, then pull those files to my host machine which houses the source(.py) .. and do the actual coverage reporting.
Is this possible currently?
[Edit] Thanks to Ned's advice, I looked into the [paths] usage, and it worked exactly how I needed it to.

Coder on Matlab to C++ , sym and perms function?

I am discovering the coder tool in Matlab. some of my code was successfully converted but it fails in functions which contain the functions "sym" for symbolic and "perms" for permutations. Also I seem to get an error when I save the answer "ans" of for example "A==B". Any idea how to solve this problem?
Thank you for your help
Here is an example of parts of my matlab function that cannot be transformed into c++ with coder:
b=4;
s=2;
one=ones(factorial(b),1);
two=2*ones(factorial(b),1);
B=perms(s+1:b+s);
S=[one,two,B];
sz=size(S);
%%%%%%%%%%%%%%%%%%%
L=[1,3;1,4;1,5;1,6;2,3;2,4;2,5;2,6];
x=perms(1:8);
M=[];
Some toolbox functions cannot be compiled, i.e. they can only be run from a MATLAB session. The following post tells us that functionality in the Symbolic toolbox cannot be compiled.
http://se.mathworks.com/matlabcentral/answers/96441-why-am-i-unable-to-compile-functions-from-the-symbolic-math-toolbox
So most likely this is the reason why you are running into problems when you try to run it in compiled form.
More info about compiler support for various toolboxes can be found here:
http://se.mathworks.com/products/compiler/supported/compiler_support.html
Symbolic Math toolbox does not appear on the list and any toolbox that is not listed is not supported (i.e. cannot be compiled).

Testing with .in files without changing code

I'm doing some programming problems from the previous year competition and in the problem text there is only one case, which is simple so I can just rewrite it when testing.
Now, I also have a folder with a bunch of .in and .out files, in format '01.in, 01.out, 02.in, 02.out, etc'.
Is there a way to somehow take one of those .in files and automatically use all the lines of it as input without making changes inside my program but rather doing it directly from the command line?
Thanks
Assuming linux:
cat *.in | yourprogram
On Windows you'd use type instead of cat.
I assume your program takes in and processes the agruments (argv[]) passed to it already. If this is the case, one way could be to write a simple wrapper program (in Python for example) which opens the required .in files, reads the lines in it and invokes your C++ program by passing the required lines as input to them.
Then you can execute this python program or make changes to it as required.

Looking for executable-based test framework

I've inherited an old program that works, but has pretty ugly source code. I need to make the code nicer without changing the functionality. This program takes an input file, performs all sorts of calculations and generates an output file.
The program is currently written in a C/C++ combination. At first I'm going to keep it a C++ program, but in the not-so-far future I'm going to convert it, or parts of it, to Python.
Naturally, the original developers haven't taken the time to create unit tests, or any other kind of test. Since I want to make sure my modifications haven't changed the program's behavior, I want to start by creating some tests. These will not be unit tests, but rather tests of the entire program.
I want each test to take one input file and a set of command line arguments, run the program and compare the output (which is the output file, stdout output and stderr output) to the expected output.
Since I need to support both C++ and Python, the test framework needs to be language agnostic - it should be able to run an executable, collect stdout and stderr and compare them, as well as another file, to the prerecorded outputs.
I couldn't find a test framework that can do that. Is there anything like that? I'd rather not develop one myself.
Well, off the top of my head, you could certainly run the executable with your desired inputs in python using subprocess or some similar module, parse the output and then use the unittest module to set expectations on what sort of output you're looking for.

Use User Input/Text File as Code

Is it possible to treat user input as lines of code in C++?
Or write it to a text file, then run that as c++ code?
I'm writing my own version of matlab, and right now it can do a lot of the numerical stuff, but only if I write the commands in like a script in main. Is there a way I could do it like matlab's command line?
Yes, there are many libraries that allow for scripting like Lua or Chaiscript which will allow you to take a file of code and execute it. There's also the advantage of being able to make changes to the code and not having to recompile each time.