Hi Miller, actually I don't know how to do it by the 1st way and thats why asked in the forum.
Since, I had no solution until someone said me that you should ask that question in forum/mailing list.
Anyway, what I currently have, I can show you.
I made a
OctPaser class.
-- OctParser.h
#ifndef OCTPARSER_H
#define OCTPARSER_H
#include <string>
using namespace::std;
class OctParser{
public:
OctParser();
~OctParser();
string eval(const string& command);
void exit();
private:
};
#endif //OCTPARSER_H
--OctParser.cpp
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/toplev.h>
#include <octave/ov.h>
#include "OctParser.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace::std;
/******************************************************************************
****************************** Constructor ************************************
******************************************************************************/
OctParser::OctParser(){
char *oct_argv[3] = {(char*)"embeded",(char*)"-q",(char*)"--interactive"};
octave_main(3,oct_argv,true);
}
OctParser::~OctParser(){
}
/******************************************************************************
**************************** Public Functions *********************************
******************************************************************************/
string OctParser::eval(const string& function) {
int status = 0;
ofstream ScriptFile("script", ios::out);
ScriptFile << function ;
ScriptFile.close();
source_file("script");
//eval_string(function,false,status);
octave_value f = eval_string("num2str(ans,10);",false,status);
string val = f.string_value();
return val;
}
void OctParser::exit(){
clean_up_and_exit (1);
// reset();
}
Now let say, I have to compute the following " x=1; y=5;z= x+y; sin(z)". Now what I do is write in a string and then write in a"script" file using and then evaluate it [look at the function] . An example is shown below.
main.cpp
OctParser Evaluate = OctParser();
string function = " x=1; y=5; x+y"
string result =Evaluate.eval(function);
result
result = 0.07845
This, all works well. The only problem is that I need to generate a "script" file each time I do any of these computations.
Now, what I want is the following.
- I do not want to generate any file and then pass on the octave parser. This is very slow and takes lot of time.
- I want a function that do not need to generate any external file. i.e I would like a function such as
- FUNCTION(string) -> directly giving me result
- Also, in the string I should be able to use any function of octave or from any external file, which I am currently able to use through my approach as shown above.
Thanks
Sumeet