How can I import a function of Octave to MATLAB? - matlab

I am running my code in Matlab. But I also want to call a function in octave. How should I import the qp function of Octave to Matlab?

The Octave language is a superset of the Matlab language. If qp only made use of the Matlab language, then you could simply add it to your Matlab path and be done with it.
However, Octave's qp makes extensive use of Octave only language so you basically have to port the code yourself. There are no tools for this, you have to convert the code from one language to another. In addition, the actual solver is the function __qp__ which is written in C++ and uses liboctave.
Two easier alternatives than porting qp are:
save the data from your Matlab session into a file save foo.mat mydata, call Octave to do the work and save the results system ('octave --eval ''load ("foo.mat"); qp (...); save foo.mat ...;', and read the file back load foo.mat.
or the much simpler alternative, just use Octave.

Octave syntaxt is not totally compatible with MATLAB. For example the preferred syntax for defining function in Octave is like this:
function ret = f()
%do something
endfunction
but MATLAB doesn't accept that syntax and there are other differences like differences in calling native codes and ... so it is not simple to convert each statement of octave library to matlab or convert oct c++ source to mex.
A straightforward way is that you should have an installation of Octave and run octave script from it then save the results to a mat file and in MATLAB load the file . You may use system function to execute octave or run it from shell.

so lets say you have 2 files in the same directory. a.m and b.m In the script b.m if you type a as a line of code everything in a.m will happen (variable assignments function definitions computations etc...)
Additionally you can use the import statement for adding things to your import list. as seen here.

Related

How do I convert Mathematica code into Matlab?

I'm just starting to learn MatLab.
This is what I'd like to convert into MatLab code:
http://postimg.org/image/jqdvcrbod/
Since a lot of my variables are functions or other variables, does that mean that when I write them as functions in MatLab I have to save each of the functions as a separate file? Is there any other way? I don't want to end up with a million separate files, but as of right now, if I write more then one function in the editor, it starts getting confused and doesn't recognize the second function.
Also, is there a way to use actual symbols (like the square root symbol instead of writing "sqrt()") like in Mathematica? I feel like long equations (like the last one) are easier on the eye that way, but that's purely aesthetics.
Is there a way to have MatLab output unassigned variables? Like in Mathematica, if I have y(x) = 2x, if i don't put anything for x, it just outputs 2x.
One quick way to do is use a package in Mathematica called ToMatlab (I attached the download link). It is able to convert Mathematica symbol syntax to Matlab .m file.
Hope this would help.

Call dll function from matlab

I have an m file from which I use to create dll using the Matlab deploytool. the code simply reads as:
function hello
disp('Hello')
end
there are six functions in the compiled dll exported as:
uint8 helloInitialize
[uint8, voidPtr, voidPtr] helloInitializeWithHandlers(voidPtr, voidPtr)
helloPrintStackTrace
helloTerminate
uint8 mlfHello
[uint8, MATLAB arrayPtr, MATLAB arrayPtr] mlxHello(int32, MATLAB arrayPtr, int32, MATLAB arrayPtr)
Now I want to run this dll from my matlab command window using calllib and use
the hello function. Assuming that I use the correct function mlfHello, calllib('hello','mlfHello') gives me nothing. Please advise me on what function to call and how to do it?
I'm not 100% its still the case but it certainly used to be that you couldn't load DLL's which were created in Matlab back into Matlab.
I suspect its still the case - so you cant do what your trying to do.
[edit] I dont have a link because they dont like to advertise the fact. The reason AFAIK is to avoid users compiling toolbox capability into DLL and giving to others to use in Matlab without a toolbox license.

Double integration using MEX

I need to write a MEX file to evaluate the double integral of an arbitrary function, since in Matlab the numerical integration is too slow.
So far I was only able defining by hand all the possible functions that I use and parse a string in the MEX file to choose which one to integrate. This is very ugly.
I would like to send to the MEX file directly the function, something like
myIntMex(#(x,y)f,0,1,0,1)
to integrate function f in the interval [0,1]x[0,1].
So far the only option I have found is mexCallMATLAB but I have also read that it is even slower than pure Matlab.
I have also found a free code here but it only works for single integrals.
So my question is: how do I do it? How do I give to the MEX file the function handle? Does a code for that already exists?

Communication between Fortran and Matlab

I am relatively new to making different programing languages communicate with each other and would appreciate some help.
Basically I have a Fortran code and a Matlab code. Both codes are first initialized and then have to run sequentially. Each code requires input from the other one. When this process has repeated often enough some convergence criteria is reached and the iteration is terminated. To make things more complicated the Fortran code not only requires input from Matlab but also from its own previous iteration. The same holds true for Matlab. So as far as I can see it is best to keep both programs open throughout the iteration process as I have a lot of variables and therefore can’t just write them in a text file to hand them over to the other programme and preserve them for the next iteration.
So essentially I’m trying to do something like this:
Initialise variable sets A,B,C and D
Fortran:
Input: A and B
Calculations …
Output: A (variables have now new values) and D
Matlab:
Input: C and D
Calculations …
Output: C (variables have now new values) and B
Repeat Fortran and Matlab until convergence criteria is reached.
So my questions are: How to make Matlab and Fortran communicate with each other and pass variables to one and another? And how can each code trigger the other one but then wait for the other code to finish its calculation first before continuing?
The keywords for your favourite search engine are "fortran mex". MATLAB has a very good documentation/tutorial, you can start here:
A MEX-file lets you call a Fortran subroutine from MATLAB
But I believe it only works if you call Fortran subroutines from Matlab. You cannot easily call Matlab .m function from Fortran code. So your "main" program has to be Matlab .m script which calls Fortran subroutines defined in the MEX file (which is actually a dynamic library).

Is there a GNU Octave equivalent for the Matlab function "fit"?

My teacher in the signal analysis course has given me some Matlab code that I have to execute in order to complete a home assignment. I have always been using GNU Octave without troubles, but this time there is this command that is giving me headaches.
[c8,g8]=fit(time, sin_4_harmonic,’fourier8’)
I cannot find the function "fit" in GNU Octave, which is referenced for Matlab at the following url http://www.mathworks.se/help/curvefit/fit.html
Does anyone knows which package should I load, or if there is any equivalent?
Thanks =)
As far as I know, that function is not present in any of the Octave packages. However, the best place to look for something similar would be the optim package, probably the function nonlin_curvefit. Looking at the documentation, the model fourier8 is of the type Y = a0+a1*cos(x*p)+b1*sin(x*p)... +a8*cos(8*x*p)+b8*sin(8*x*p).
A work-around may be using "polyfit" function. To get the values, use "polyval".