Performing system calls from MATLAB Mex files - matlab

In MATLAB itself, it is very easy to call system commands, such as the following:
>> system('ls');
yprime.c yprime.mexa64
(Note: using Ubuntu)
From C(++) programs, I can execute system commands using std::system:
#include <cstdlib>
int main()
{
std::system("ls");
}
But how can I execute system commands from MATLAB Mex programs?
#include <cstdlib>
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
std::system("ls");
}
^^The above compiles, but doesn't output anything to screen when run.

I think you should have a look at that thread:
Capturing stdout from a system() command optimally
#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);

Related

How do I use Gio library in Vala?

I'm building a Gtk application in Vala and would like to use the Gio library and more specifically the g_app_info_get_all() function. I am able to achieve this in C as follows:
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <gio/gdesktopappinfo.h>
int main(int argc, char *argv[])
{
...
GDesktopAppInfo *app_info;
GList *app_list, *l;
gtk_init(&argc, &argv);
...
app_list = g_app_info_get_all();
...
}
How do I achieve the same in Vala?
ChatGPT suggested me to use Gio.AppInfo.get_all()
and even though
pkg-config --modversion gio-2.0 returns 2.74.3 but using Gio; throws an error saying the namespace 'Gio' could not be found even when I'm compiling with --pkg gio-2.0 flag.
The namespace is GLib, this is shown as {} Glib in Valadoc. The GLib namespace is used by default, so the following will work:
void main () {
var result = AppInfo.get_all ();
foreach (var app in result) {
print (#"$(app.get_display_name())\n");
}
}
Compile with:
valac example.vala --pkg gio-2.0

How to solve: error C2039: 'make_normal_of_point_with_normal_pmap': is not a member of 'CGAL'

I am using CGAL 4.12 and eigen 3.3.4 and trying to compile the Poisson_surface_reconstruction_3 example trough a Matlab mex function and am getting the following error:
C:\Users\u0116401\Documents\PRosPeRoS\Matlab_Code\mexTest\CGAL_poisson_reconstruction.cpp(70): error C2039: 'make_normal_of_point_with_normal_pmap': is not a member of 'CGAL'
C:\dev\CGAL-4.12\include\CGAL/IO/read_xyz_points.h(40): note: see declaration of 'CGAL'
C:\Users\u0116401\Documents\PRosPeRoS\Matlab_Code\mexTest\CGAL_poisson_reconstruction.cpp(70): error C3861: 'make_normal_of_point_with_normal_pmap': identifier not found
It seems 'make_normal_of_point_with_normal_pmap' can't be found. Does anyone know what is causing this issue?
The code that produces this error is:
/* mex headers */
#include <mex.h>
/* C++ headers */
#include <vector>
#include <fstream>
/* CGAL headers */
#include <CGAL/trace.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/Surface_mesh_default_triangulation_3.h>
#include <CGAL/make_surface_mesh.h>
#include <CGAL/Implicit_surface_3.h>
#include <CGAL/IO/output_surface_facets_to_polyhedron.h>
#include <CGAL/Poisson_reconstruction_function.h>
#include <CGAL/Point_with_normal_3.h>
#include <CGAL/property_map.h>
#include <CGAL/IO/read_xyz_points.h>
#include <CGAL/compute_average_spacing.h>
// Types
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::FT FT;
typedef Kernel::Point_3 Point;
typedef CGAL::Point_with_normal_3<Kernel> Point_with_normal;
typedef Kernel::Sphere_3 Sphere;
typedef std::vector<Point_with_normal> PointList;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;
typedef CGAL::Poisson_reconstruction_function<Kernel>Poisson_reconstruction_function;
typedef CGAL::Surface_mesh_default_triangulation_3 STr;
typedef CGAL::Surface_mesh_complex_2_in_triangulation_3<STr> C2t3;
typedef CGAL::Implicit_surface_3<Kernel, Poisson_reconstruction_function> Surface_3;
void mexFunction(int nlhs, mxArray *plhs[], /*Output variables */
int nrhs, const mxArray *prhs[]) /*Input variables */
{
PointList points;
std::ifstream stream("kitten.xyz");
if (!stream ||
!CGAL::read_xyz_points_and_normals(
stream,
std::back_inserter(points),
CGAL::make_normal_of_point_with_normal_pmap(std::back_inserter(points))))
}
The function is named make_normal_of_point_with_normal_map() (map not pmap) and it takes Point_with_normal as parameter. The call should be:
CGAL::make_normal_of_point_with_normal_map(Point_with_normal())

How to add an header file in a C project on Eclipse?

I real need help over here, I have to do this ASCIITwitter project for an university's exam and I'm stucked with this problem:
I have to add a header file and a source file on my project of course, so first I tried some easy code to see if I'm capable to do this.
Just a program to do some square operations:
There's my code:
Twitter.h
#ifndef TWITTER_H_
#define TWITTER_H_
int square(int);
#endif /* TWITTER_H_ */
Twitter.c
#include "Twitter.h"
int square(int x)
{
return x*x;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Twitter.h"
int main(void)
{
int y;
y=square(5);
printf("%d\n",y);
system("PAUSE");
}
But it give to me "undefined reference to 'square' and I really don't know how to fix this. I've tried searching on internet, but I'm working on windows, I don't have any makefile, I just want to make this work. Please help me.

How MATLAB mex files access MATLAB instances?

This is the entry point for every mex file:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);
In fact mex files are windows dll files with mexFunction as the main function.
My question is when the mex function is called, how it can access matlab instance specific data from inside the mex. As an example, consider 'mexPutVariable' function. It's job is 'copy an array from inside MEX-function into the specified workspace (outside mex)'. But how it knows where is 'workspace'. No parameter has passed to mex (like a pointer), containing matlab instance data (caller). mex files know only nlhs, plhs, nrhs, prhs and none of them can help mex files to excavate matlab instance specific data(caller function information).
A possible solution is that "Matlab.exe" is declaring mexPutVariable as an exported function:
[Matlab.exe]
int __declspec(dllexport) mexPutVariable(const char* workspace, const char* name, const mxArray* parray)
{
...
}
It is then very easy to retreive this function from the dll using GetModuleHandle and GetProcAddress:
[Module.dll]
// Declare function pointer
int (*FctnPtr)(const char* workspace, const char* name, const mxArray* parray);
// Retreive the main executable
HANDLE hExe = GetModuleHandle(NULL);
// Link to exported function in the exe just like you would do for any dll
FctnPtr mexPutVariable = (FctnPtr)GetProcAddress(hExe, "mexPutVariable");
// Use exported function from the dll
mexPutVariable("Base", "foo", myArray);
For compiling the mex file and after looking at mex.h file, mexPutVariable is declared as an external function to link with:
LIBMWMEX_API_EXTERN_C int mexPutVariable(const char* workspace, const char* name, const mxArray* parray);
Which turn to be simply (when compiled as dll side):
extern "C" int mexPutVariable(const char* workspace, const char* name, const mxArray* parray);

Why VS2015 intellisense shows error on C++11 user defined literals (UDL)

The below code can be compiled and run, but VS2015 intellisense shows error. g++ & eclipse has the same issue (compiled & run but shows error)
Does anyone know how to fix it? I tried searching on google but hopeless.
The error is a little annoying.. :-)
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
using namespace std::literals;
using namespace chrono_literals;
int main()
{
this_thread::sleep_for(5s);
cout << "test \n";
return 0;
}
Error message: "Invalid suffix 's' on integer literal"
Thanks a lot!
You should add some #include statements and namespace references:
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
using namespace std::literals::chrono_literals;
std::this_thread::sleep_for(5s);
std::cout << "test \n";
return 0;
}
In your code, the compiler is not been told to use namespace std. The 5s does not work without std::literals