mexPrintf does not display anything - octave

I am trying to use MEX-files in Octave. For starters, I'm simply trying to call the MEX-file so it can print "this is a test". My mexfile.cpp is:
#include "mex.h"
void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mexPrintf ("Hello, World!\n");
return;
}
and I'm trying to call the file by entering the following into the command window:
mkoctfile --mex mexfile.cpp;
It appears the file is compiling but nothing is being printed. Any idea as to why that is the case?

Related

Don't know how to create a function file cpp on visual studio (c++)

I am new in c++ language and have a problem in how to create a file for a function to use it (I know I can create it within a class I just need to create cpp file and header file for a function so I can use it within the main function like this screen shots enter image description here
This is my code that cannot run because as I think I didn't declare the function in a separate cpp file so it be treated as object and the linker link them together(I think that based on my poor knowledge )
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
int PassByValue(int a);
int main()
{
int num1 = 1;
int *pNum = new int;
*pNum = 5;
int PassByValue(int a){
string op = "The name of this fuction is PassByValue";
cout << op << endl;
return 0;
}
return 0;
}
How I can use the function without getting compiler errors (c1010--c2601)
can any one tell me whats wrong with the code and how to fix it?
With my regards

C++ the returned array value is not correct

First of thanks for giving me a hand with this. I am no expert at C++ but i have done some work in C. My code problem is that it would not display the returned array value correctly.
In general what my program trying to do is to evaluate a function F(x) , display it in a table format and find its min and max. I have find ways of doing all that but when I want to display the returned value of array F(x) it somehow got distorted.The first value is always correct for example like
cout << *(value+0) <<endl;
but the next one the value is not the same as the supposed f(x).Sorry in advance if my code is not up to the proper standard but i been wrapping my head over this for awhile now.
My Full Code
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <string>
#include <stdlib.h>
using namespace std;
float *evaluate ();
void display ();
void Min_Max(float *);
int main()
{
float *p;
evaluate();
display();
cin.get();
p = evaluate();
Min_Max(p);
return 0;
}
float *evaluate()
{
ofstream Out_File("result.txt");
int n=30;
float x [n];
float fx[n];
float interval = ((4-(-2))/0.2);
x[0]= -2.0;
for(n=0;n <= interval;n++)
{
fx[n] = 4*exp((-x[n])/2)*sin((2*x[n]- 0.3)*3.14159/180);
x[n+1] = x[n] + 0.2;
if (Out_File.is_open())
{
Out_File <<setprecision(5)<<setw(8)<<showpoint<<fixed<< x[n];
Out_File << "\t\t"<<setprecision(5)<<setw(8)<<showpoint<<fixed<<fx[n]<<endl;
}
else cout << "Unable to open file";
}
Out_File.close();
return fx;
}
void display()
{
ifstream inFile;
inFile.open("result.txt");
string line;
cout << " x\t\t\t f(x)"<<endl;
cout << "_______________________________________"<<endl;
while( getline (inFile,line))
{
cout<<line<<endl;
}
inFile.close();
}
void Min_Max(float *value)
{
int a=0;
for(a=0;a<=30;a++){
cout << *(value+a) <<endl;
*value =0;}
}
I see, you pass p to your function Min_Max. Where p is a pointer to an entry point of an array. That array is created as a local variable in another function evaluate. That doesn't work, because as soon as evaluate has finished, all its local variables, such as the fx array, get destroyed and the pointer you return then points to "nothing".
In that case you can use heap memory (use new operator) to allocate the fx. But don't forget to free it afterward.
Also, look here

Debugging CUFFTW interface plan creation

I am begining to port an existing fftw3 application to make use of the cuda fftw libraries. The initial stage is to simply replace the fftw3.h header with the cufft.h header and link the cufft libraries instead of the fftw3 libraries.
That is simple enough, and the code compiles with nvcc. However when I execute the code the application is unable to create a plan using the fftw_plan_guru_dft command (it just returns 0 instead of a valid plan).
Since there are no errors reported I am at a loss as to how I might debug this issue. cuda-gdb and gdb do not provide any further insight. They simply report
Error: Internal error reported by CUDA debugger API (error=7). The application cannot be further debugged.
UPDATE: So here is the minimum working example. As mentioned in my comment to Talonmies, this code is autogenerated by a scientific differential equation solver. So please excuse the function names etc.
#define real Re
#define imag Im
#include <complex>
#undef real
#undef imag
#include <cufftw.h>
#include <stdio.h>
int main(void) {
int _transform_sizes_index = 1, _loop_sizes_index = 0;
fftw_iodim _transform_sizes[1], _loop_sizes[2];
_transform_sizes[0].n = 128;
_transform_sizes[0].is = 0;
_transform_sizes[0].os = 0;
fftw_complex _data_in[128] = {0.};
static fftw_plan _fftw_forward_plan = NULL;
_fftw_forward_plan = fftw_plan_guru_dft(
_transform_sizes_index, _transform_sizes,
_loop_sizes_index, _loop_sizes,
reinterpret_cast<fftw_complex*>(_data_in),
reinterpret_cast<fftw_complex*>(_data_in),
FFTW_FORWARD, FFTW_PATIENT);
if (!_fftw_forward_plan)
printf("Error: Unable to create forward plan\n");
return 0;
}
Unless anyone else knows what I am doing wrong, it looks like this particular functionality of fftw3 may not be supported by cufftw.
As talonmies pointed out, the fftw_plan_guru_dft only has partial support in the cufftw library. The above example will run if you instead make use of the basic level fftw_plan_dft. More concretely
#define real Re
#define imag Im
#include <complex>
#undef real
#undef imag
#include <cufftw.h>
#include <stdio.h>
int main(void) {
int _transform_sizes_index = 1, _loop_sizes_index = 0;
int _transform_sizes[1] = {128};
fftw_complex _data_in[128] = {0.};
static fftw_plan _fftw_forward_plan = NULL;
_fftw_forward_plan = fftw_plan_dft(
_transform_sizes_index, _transform_sizes,
reinterpret_cast<fftw_complex*>(_data_in),
reinterpret_cast<fftw_complex*>(_data_in),
FFTW_FORWARD, FFTW_PATIENT);
if (!_fftw_forward_plan)
printf("Error: Unable to create forward plan\n");
return 0;
}

Mingw, Cygwin g++, STL set

I tried to remove element from STL set data structure by iterating each element. The problem is that I can't delete each element by iterating set structure. When I tried that way, it just give error message or program is stuck. When it was happened, I thought it was Mingw's bug, so I tried Cygwin g++; however Cygwin g++ also didn't work.
How should I change the code to delete each element in set structure by using iteration
MinGw and Cygwin are installed on windows 7 x64 system.
//Tested by using MinGW, Cygwin g++ in Win64 environment
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main()
{
set<int> mySet;
mySet.insert(1);
mySet.insert(2);
mySet.insert(7);
/*
//Works fine
mySet.erase(1);
mySet.erase(2);
mySet.erase(7);
*/
/*
//Error
for(set<int>::iterator iter = mySet.begin(); iter != mySet.end(); iter++)
mySet.erase(iter);
//Error
int size = mySet.size();
set<int>::iterator iter = mySet.begin();
for(int i = 0; i < size(); i++)
{
mySet.erase(iter);
iter++
}
*/
//print out elements in mySet
for(set<int>::iterator iter = mySet.begin(); iter != mySet.end(); iter++)
cout << *iter << " ";
return 0;
}
Like Igor Tandetnik sayed mySet.clear() will work.
But normally in C++11, mySet.erase(iter) should return the following element:
Return an iterator to the element that follows the last element removed (or set::end, if the last element was removed).
In C++98 it returns nos value, that is why it doesn't work.
Try to pass -std=c++11when you'll compile to test.
Source : http://www.cplusplus.com/reference/set/set/erase/

for_each bind vector of vector resize

This is my first question. I gave up and will use a hand rolled functor for this, but I am curious as to how it is supposed to be done. The contrived example below is intended to resize all of the vectors in a vector to be of size 9, by filling them with nulls. The indicated line causes MinGW GCC 4.5.0 to spew a lot of template errors. I've tried several different permutations, but only posted the code that I consider to be "closest to correct" below. How should it be written? Note, I want to retain the two-argument version of resize.
#include <vector>
using std::vector;
#include <algorithm>
using std::for_each;
#include <tr1/functional>
using std::tr1::bind;
using std::tr1::placeholders::_1;
int main() {
vector<vector<void *> > stacked_vector(20);
for_each(stacked_vector.begin(),stacked_vector.end(),
bind(&std::vector<void *>::resize,_1,9,0/*NULL*/)); // voluminous error output
return 0;
}
Thank you very much for your input.
It's hard to say without seeing the error output (and frankly, even with it). However, try passing the NULL as a void* type: static_cast<void*>(0). Otherwise the object returned by bind tries to give an int value as the second parameter to resize.
Try this.
#include <functional>
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
typedef std::vector<int> vec_int;
typedef std::vector<vec_int> vec_vec_int;
// Do this to make the _1 work
using namespace std::placeholders;
static const int FIRST_DIM = 5;
static const int SECOND_DIM = 10;
static const int DEFAULT_VALUE = 66;
vec_vec_int v(FIRST_DIM);
std::for_each(v.begin(), v.end(),
std::bind(&vec_int::resize, _1, SECOND_DIM, DEFAULT_VALUE));
std::cout << v[4][9];
return (0);
}
If you do not want to specify the default value, you do not need to.