Results 1 to 15 of 15

Thread: Want to run Octave m-file

  1. #1
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Want to run Octave m-file

    Hello!

    I have an octave (MATLAB clone) script that I want to call from my Qt C++ code.
    According to this wiki it is quite possible to do.
    Though I can't get it to work properly...

    What I would like to start with is a simple GUI where I can enter a few variables that gets sent to the octave-script when I push a button, returns a few values and write them into textlables or something like that.

    Or perhaps we should just get the example to work in it's terminal form first.
    Here is what I have done:
    1. Start a new Qt4 console project.
    2. Created the example m-file from the link and put it in the project directory.
    3. Opened main.cpp and kept the include QCoreApplication and changed the rest of the content for the one in the linked example above.
    4. When I try to build I get the error " 'cout' is not a member of 'std' ".
    5. I tried including iostream and it stopped complaining about cout (Why wasn't it there in the first place?) but it has two warnings: Unused parameters 'argc' and 'argv' and 25 blue question marks saying "undefined reference to..."
    Does this mean that the include-files doesn't contain the definitions that the code is referring to?
    The last line of the Build issues is an error and says "Id returned 1 exit status"

    So, anyone up for giving a true Qt/C++ noob a little help?
    btw. I'm on Ubuntu 9.10 and the latest Qt Creator.

    Thank you.
    Have a nice day!
    /Tottish

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Want to run Octave m-file

    I have Octave installed on my Debian machine although I've never used it. If you post your code I'll try to help.

    I did notice that there is a package named "qtoctave" in the Debian repositories. If that is not what you are looking for you could download the source code and maybe get some ideas about what to do.

  3. #3
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Want to run Octave m-file

    Hi!
    Thanks for responding!

    Qtoctave is just a GUI for octave. If I was a decent C++-programmer I'm sure the sourcecode could be very useful. However, I'm not so I'll try it this way instead since I have what seems to be a complete example here.
    My sourcecode is just copied and pasted from the link to the wiki. With the few exceptions mentioned in the post above.

    Cheers!
    /Tottish

    EDIT:
    Here is the code:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <iostream>
    3. #include <octave/oct.h>
    4. #include <octave/octave.h>
    5. #include <octave/parse.h>
    6. #include <octave/toplev.h> /* do_octave_atexit */
    7.  
    8. int main (const int argc, char ** argv)
    9. {
    10. const char * argvv [] = {"" /* name of program, not relevant */, "--silent"};
    11.  
    12. octave_main (2, (char **) argvv, true /* embedded */);
    13.  
    14. octave_value_list functionArguments;
    15.  
    16. functionArguments (0) = 2;
    17. functionArguments (1) = "D. Humble";
    18.  
    19. Matrix inMatrix (2, 3);
    20.  
    21. inMatrix (0, 0) = 10;
    22. inMatrix (0, 1) = 9;
    23. inMatrix (0, 2) = 8;
    24. inMatrix (1, 0) = 7;
    25. inMatrix (1, 1) = 6;
    26.  
    27. functionArguments (2) = inMatrix;
    28.  
    29. const octave_value_list result = feval ("exampleOctaveFunction", functionArguments, 1);
    30.  
    31. std::cout << "resultScalar is " << result (0).scalar_value () << std::endl;
    32. std::cout << "resultString is " << result (1).string_value () << std::endl;
    33. std::cout << "resultMatrix is\n" << result (2).matrix_value ();
    34.  
    35. do_octave_atexit ();
    36.  
    37. }
    To copy to clipboard, switch view to plain text mode 

    EDIT2:
    Hmmm if I add the line
    QCoreApplication a(argc, argv);
    First thing in the body then build issues are restricted to QcoreApplication.. Am I on to something?...
    Last edited by Tottish; 31st March 2010 at 18:19.

  4. #4
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Want to run Octave m-file

    Couple things:
    You are currently not actually writing a Qt program, since as you've noticed you don't have a QApplication. Create one first (QApplication app(argc, argv)

    The warnings you can ignore.
    The errors about undefined references.... Might be that you have to include the lib. Do the octave docs say anything about that? Where are those warnings?

  5. #5
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Want to run Octave m-file

    Also, the octave docs show the makefile necessary to build that example. In QtCreator, you need to add the relevant parts to your project file. Most importantly the include and lib dirs. I would try adding something like this to your .pro file (at the end) and then try doing a clean and rebuild.

    INCLUDEPATH += /usr/include/octave-3.0.1/ # MAKE SURE THIS PATH EXISTS or change it to your system.
    LIBS += -L"/usr/lib/octave-3.0.1/" -loctinterp

    Make sure those paths exist.

  6. #6
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Want to run Octave m-file

    OK, thanks a lot for those pointers pherthyl!
    I will look into it first thing tomorrow. Right now my tired brain needs some sleep.
    Much appreciated!

    /Ttotish

  7. #7
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Want to run Octave m-file

    OK, my new .PRO file:
    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2010-03-31T15:03:01
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT -= gui
    8.  
    9. TARGET = test2
    10. CONFIG += console
    11. CONFIG -= app_bundle
    12.  
    13. TEMPLATE = app
    14.  
    15.  
    16. SOURCES += main.cpp
    17.  
    18. INCLUDEPATH += /usr/include/octave-3.2.2/
    19. LIBS += -L"/usr/lib/octave-3.2.2/" -loctinterp
    To copy to clipboard, switch view to plain text mode 


    The new code: (Not sure if the last line should be there but I guess something should be returned?)
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <iostream>
    3. #include <octave/oct.h>
    4. #include <octave/octave.h>
    5. #include <octave/parse.h>
    6. #include <octave/toplev.h> /* do_octave_atexit */
    7.  
    8. int main (int argc, char *argv[])
    9. {
    10. QCoreApplication a(argc, argv);
    11. const char * argvv [] = {"" /* name of program, not relevant */, "--silent"};
    12.  
    13. octave_main (2, (char **) argvv, true /* embedded */);
    14.  
    15. octave_value_list functionArguments;
    16.  
    17. functionArguments (0) = 2;
    18. functionArguments (1) = "D. Humble";
    19.  
    20. Matrix inMatrix (2, 3);
    21.  
    22. inMatrix (0, 0) = 10;
    23. inMatrix (0, 1) = 9;
    24. inMatrix (0, 2) = 8;
    25. inMatrix (1, 0) = 7;
    26. inMatrix (1, 1) = 6;
    27.  
    28. functionArguments (2) = inMatrix;
    29.  
    30. const octave_value_list result = feval ("exampleOctaveFunction", functionArguments, 1);
    31.  
    32. std::cout << "resultScalar is " << result (0).scalar_value () << std::endl;
    33. std::cout << "resultString is " << result (1).string_value () << std::endl;
    34. std::cout << "resultMatrix is\n" << result (2).matrix_value ();
    35.  
    36. do_octave_atexit ();
    37. return a.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 


    Here is the compiler output: (After a clean and attempt to build)
    Qt Code:
    1. Running build steps for project test2...
    2. Configuration unchanged, skipping QMake step.
    3. Starting: /usr/bin/make -w
    4. make: Entering directory `/home/snurr/octaveTest/qttest/Qt-call-m/test2'
    5. g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_CORE_LIB -DQT_SHARED -I../../../../qtsdk-2010.02/qt/mkspecs/linux-g++ -I. -I../../../../qtsdk-2010.02/qt/include/QtCore -I../../../../qtsdk-2010.02/qt/include -I/usr/include/octave-3.2.2 -I. -o main.o main.cpp
    6. g++ -Wl,-rpath,/home/snurr/qtsdk-2010.02/qt/lib -o test2 main.o -L/home/snurr/qtsdk-2010.02/qt/lib -L/usr/lib/octave-3.2.2/ -loctinterp -lQtCore -L/home/snurr/qtsdk-2010.02/qt/lib -lpthread
    7. /usr/bin/ld: warning: liboctave.so, needed by /usr/lib/octave-3.2.2//liboctinterp.so, not found (try using -rpath or -rpath-link)
    8. /usr/bin/ld: warning: libcruft.so, needed by /usr/lib/octave-3.2.2//liboctinterp.so, not found (try using -rpath or -rpath-link)
    9. main.o: In function `main':
    10. /home/snurr/octaveTest/qttest/Qt-call-m/test2/main.cpp:28: undefined reference to `MatrixType::MatrixType()'
    11. /home/snurr/octaveTest/qttest/Qt-call-m/test2/main.cpp:28: undefined reference to `MatrixType::~MatrixType()'
    12. /home/snurr/octaveTest/qttest/Qt-call-m/test2/main.cpp:28: undefined reference to `MatrixType::~MatrixType()'
    13. ......
    14. LOTS OF errors about undefined references here. My guess is because it cant find the .so-files?
    15. .......
    16. /usr/lib/octave-3.2.2//liboctinterp.so: undefined reference to `operator*(DiagMatrix const&, ComplexDiagMatrix const&)'
    17. /usr/lib/octave-3.2.2//liboctinterp.so: undefined reference to `mx_el_ge(octave_int<unsigned int> const&, intNDArray<octave_int<long long> > const&)'
    18. /usr/lib/octave-3.2.2//liboctinterp.so: undefined reference to `MArrayN<octave_int<unsigned char> > quotient<octave_int<unsigned char> >(MArrayN<octave_int<unsigned char> > const&, MArrayN<octave_int<unsigned char> > const&)'
    19. collect2: ld returned 1 exit status
    20. make: *** [test2] Error 1
    21. Exited with code 2.
    22. Error while building project test2
    23. When executing build step 'Make'
    To copy to clipboard, switch view to plain text mode 

    This part I don't understand:
    /usr/bin/ld: warning: liboctave.so, needed by /usr/lib/octave-3.2.2//liboctinterp.so, not found (try using -rpath or -rpath-link)
    /usr/bin/ld: warning: libcruft.so, needed by /usr/lib/octave-3.2.2//liboctinterp.so, not found (try using -rpath or -rpath-link)
    The files are indeed located in the folders specified...??? (EDIT. Clarification: They are located in the same folder as liboctinterp.so... not in /usr/bin...)
    Tried using -rpath and rpath-link aftre/instead of the - locinterp line but then g++ says "unrecognized option"

    So what should I do now?
    Thank you for your help! I really appreciate it!
    /Tottish
    Last edited by Tottish; 1st April 2010 at 09:12.

  8. #8
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Want to run Octave m-file

    I started out trying to run the octave example as a c++ file(i.e. no Qt) and received the same errors. The problem is you have to tell the linker where the libs are located. As root, run "ldconfig /path/to/octave/libs". This is not permanent because the next time ldconfig is run it will not find the octave libs.

    To make sure the cache gets updated every time ldconfig is run (Ubuntu is a Debian derivative so I'm assuming the procedure is the same) do this:

    1. sudo -i
    2. cd /etc/ld.so.conf.d
    3. touch octave.conf
    4. vi (your fav. editor) octave.conf
    5. add a line: /path/to/octavelibs
    6. save file
    7. run ldconfig


    I got it to run as a Qt app also. The only difference was I used the QApplication class.

    HTH
    Last edited by norobro; 1st April 2010 at 15:39. Reason: correct typos

  9. The following user says thank you to norobro for this useful post:

    Tottish (1st April 2010)

  10. #9
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Want to run Octave m-file

    That's great news norobro!

    I can't really get it to work here though...
    I run "ldconfig /usr/lib/octave-3.2.2" (after sudo) and only get a new prompt (the same response if I misspell) . nothing else happens and the same errors when compiling. Maybe there should be a -l or something in the command?


    /Tottish

    EDIT:
    WoHoo!
    Got it to work! I thought that the second thing you listed also had to be done for it to work. Thought it only applied after reboot, proves how new I am to this I guess! =)
    Anyway, a HUGE thank you from me to you!!!
    Last edited by Tottish; 1st April 2010 at 17:39.

  11. #10
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Want to run Octave m-file

    Hmm, I thought that would work.
    No response is the expected behavior. It updates the cache. Try "ldconfig -p | grep octave:
    Qt Code:
    1. ldconfig -p | grep octave
    2. liboctinterp.so (libc6) => /usr/lib/octave-3.2.4/liboctinterp.so
    3. liboctave.so (libc6) => /usr/lib/octave-3.2.4/liboctave.so
    4. libcruft.so (libc6) => /usr/lib/octave-3.2.4/libcruft.so
    To copy to clipboard, switch view to plain text mode 
    I had this problem trying to implement qwt. I don't understand why putting the libs with -L and -l in the Makefile doesn't work, but I tried a lot of different combinations to no avail. Maybe someone can enlighten me(us).

  12. #11
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Want to run Octave m-file

    WoHoo!
    Got it to work! I thought that the second thing you listed also had to be done for it to work. Thought it only applied after reboot, proves how new I am to this I guess! =)
    Anyway, a HUGE thank you from me to you!!
    Still tricking around a little though but the major errors are GONE! I'll keep you posted.
    /Tottish

    EDIT: It works! Runs perfectly... Now row to implement my nonlinear optimization algorithm. =) Hmmm. I wonder if global variables work with this method... Probably not since they are most likely not contained within the library...
    Last edited by Tottish; 1st April 2010 at 18:10.

  13. #12
    Join Date
    Mar 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Want to run Octave m-file

    Quote Originally Posted by Tottish View Post
    WoHoo!
    Got it to work! I thought that the second thing you listed also had to be done for it to work. Thought it only applied after reboot, proves how new I am to this I guess! =)
    Anyway, a HUGE thank you from me to you!!
    Still tricking around a little though but the major errors are GONE! I'll keep you posted.
    /Tottish

    EDIT: It works! Runs perfectly... Now row to implement my nonlinear optimization algorithm. =) Hmmm. I wonder if global variables work with this method... Probably not since they are most likely not contained within the library...
    Hi,
    I'm working with Windows and cannot adapt your advice from the Linux-world. Sorry!
    I get lots of udefined reference errors, and don't know where to put some correct paths to libraries to solve the problem. Maybe you can help me?
    Thanks!

    Starting: C:/Qt/2010.02.1/mingw/bin/mingw32-make.exe -w
    mingw32-make: Entering directory `C:/Qt/ExamplesFromOthers/Octave_integration'
    C:/Qt/2010.02.1/mingw/bin/mingw32-make -f Makefile.Debug
    mingw32-make[1]: Entering directory `C:/Qt/ExamplesFromOthers/Octave_integration'
    g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_CORE_LIB -DQT_THREAD_SUPPORT -I"..\..\2010.02.1\qt\include\QtCore" -I"..\..\2010.02.1\qt\include" -I"..\..\..\Octave\3.2.4_gcc-4.4.0\include" -I"..\..\..\Octave\3.2.4_gcc-4.4.0\include\octave-3.2.4\octave" -I"..\..\2010.02.1\qt\include\ActiveQt" -I"debug" -I"..\..\2010.02.1\qt\mkspecs\win32-g++" -o debug\main.o main.cpp
    g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-subsystem,console -mthreads -Wl -o debug\test2.exe debug/main.o -L"c:\Qt\2010.02.1\qt\lib" -LC:\Octave\3.2.4_gcc-4.4.0\lib -loctinterp -LC:\Octave\3.2.4_gcc-4.4.0\bin -loctinterp -lQtCored4
    mingw32-make[1]: Leaving directory `C:/Qt/ExamplesFromOthers/Octave_integration'
    mingw32-make: Leaving directory `C:/Qt/ExamplesFromOthers/Octave_integration'
    debug/main.o: In function `main':
    C:\Qt\ExamplesFromOthers\Octave_integration/main.cpp:28: undefined reference to `MatrixType::MatrixType()'
    C:\Qt\ExamplesFromOthers\Octave_integration/main.cpp:28: undefined reference to `MatrixType::~MatrixType()'
    C:\Qt\ExamplesFromOthers\Octave_integration/main.cpp:28: undefined reference to `MatrixType::~MatrixType()'
    C:\Qt\ExamplesFromOthers\Octave_integration/main.cpp:34: undefined reference to `operator<<(std:stream&, Matrix const&)'
    ... etc ...

  14. #13
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Want to run Octave m-file

    Hi!
    I'm no wiz at this but to me it doesn't seem like an Octave specific problem so I'm sure you can get more experienced help from others reading this if my tip is wrong/doesn't work.

    You might need something like this in your .pro-file:
    Qt Code:
    1. OCTAVE_LOCATION = C:\path\to\octave
    2. INCLUDEPATH += $${OCTAVE_LOCATION}/path/to/sources
    3. LIBS = -L$${OCTAVE_LOCATION}/path/to/libs \
    4. -loctinterp #(or whatever the name of the library(s?) you want to use.
    To copy to clipboard, switch view to plain text mode 

    Also, try to remember to wrap the code/compiler output you post in CODE tags for increased readability and to avoid smilies. (Use the pound sign from the toolbar).
    Welcome to the Forum and Good luck! Just hang in there, google 'til your eyes bleed, read any official documentation and you'll crack it.
    Peace Out!
    /Tottish

  15. #14
    Join Date
    Dec 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Android

    Default Re: Want to run Octave m-file

    Hello, sorry to revive this old thread, but is the only reference I have found about using Qt and Octave together.

    I'm currently developing an application that uses Octave to do some signal processing to an audio signal (including loading and playing it) and Qt as the GUI.

    I'm having trouble when running octave embedded in a Qt project as one of the Octave functions used in my process, hanning() is returning only zeros.

    This doesn't happen if I embed without Qt.

    If you can help me or guide me where I can get more help, thank you very much.

  16. #15
    Join Date
    Dec 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Android

    Default Re: Want to run Octave m-file

    The problem I was having is related to the version 3.2 of Octave. Testing the program with the 3.6 version everything works fine so far.

Similar Threads

  1. Replies: 5
    Last Post: 15th June 2010, 07:42
  2. Replies: 3
    Last Post: 28th March 2009, 15:37
  3. Replies: 0
    Last Post: 6th March 2009, 08:19
  4. Replies: 2
    Last Post: 2nd April 2008, 17:28
  5. Replies: 3
    Last Post: 25th May 2007, 07:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.