I have the task to make some scripts compatible to Octave (or create counterparts that run in Octave).
I am trying to handle this matlab part:
%ft = fittype( 'a+b*y*x+c*y^2', 'independent', {'x', 'y'}, 'dependent', 'z' );
%
%opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
%opts.Display = 'Off';
%opts.Lower = [0 0 0];
%
%opts.MaxFunEvals = 10000;
%opts.MaxIter = 10000;
%opts.Robust = 'Bisquare';
%opts.StartPoint = [0.12345 0.456789 0.23456];
%[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
%coeffs=coeffvalues(fitresult);
There's obviously no direct equivalent to this toolbox from Matlab in Octave.
I can do linear and nonlinear regression, but not robust. So I looked around for alternative ways.
I found that GSL can calculate fittings using the same biweight robust alghorithm.
https://www.gnu.org/software/gsl/doc/html/lls.html?highlight=robust#c.gsl_multifit_robust_alloc
I think there are ways to call GSL libraries form Octave (the forge package gsl does not include the funtions I need).
So my questions:
a) Working on windows, can I make octave use those libraries? (I know it can call C++ functions). And can I include those libraries to give ppl with "just" octave a "package" to make it work together?
b) Any leads on how to go this way?
In the documentation it is only described how to make .oct files (c++ code) that use octave and c++ libraries. But I'd like to stay within octave code (and .m files).
Related
I have a Matlab script that I'm trying to convert to C++ (see below) because it is extremely slow. I'm a C++ newbie and to start I tried using codegen but it doesn't work (I got the message ??? This text contains non-empty top-level expressions. It appears to be a script.)
Do you have any suggestion on how to start to convert the code? Also, what is the best C++ function to do the job of fitsread?
Here is my code:
clear;
number_projections = 10;
imgs_per_proj = 2000; % Number of images per projection
% Lets load the reference images relative to the various wavelengths
R1 = zeros(imgs_per_proj, 512, 512);
R2 = zeros(imgs_per_proj, 512, 512);
l = 0;
for k = 1:imgs_per_proj
s = sprintf('Ref/R1_000_%05i.fits',k-1);
t = sprintf('Ref/R2_000_%05i.fits',k-1);
l = l + 1;
R1(l,:,:) = fitsread(s);
R2(l,:,:) = fitsread(t);
end
codegen requires that the MATLAB code you're trying to convert be in a function and it seems you are trying to convert a script
However, even if you do that, fitsread does not appear to be a supported function within codegen. Here's a list of supported functions:
http://www.mathworks.com/help/simulink/ug/functions-supported-for-code-generation--alphabetical-list.html
There's no "built-in" function within C++ that's going to replace fitsread - for that you need a library. There's a C++ FITS library called CCFits that you can find here: http://heasarc.gsfc.nasa.gov/fitsio/CCfits/
They should have tutorials that you could follow.
c = imread('focus.jpg');
I have a 3D array ( image ) in m.file ,I want to use this
array in C++,how can i pass this array to C++ file
thanks in advance
You can call MATLAB Engine directly in C++.
Check out Call MATLAB Functions from C and C++ Applications for more info and examples.
Edit: On the other hand, it seems you don't need to do this, you can simply load this image directly in C++, e.g. using OpenCV.
I am working with Visual Studio on C++ code, and I send some variables to MATLAB engine. So far there is no problem. Imagine I have the MATLAB command window opened and these variables:
» whos
Name Size Bytes Class Attributes
QWe 1x365 2920 double
QWp 1x364 2912 double
QWu 1x364 2912 double
I can use the standard MATLAB functions, but I have dowloaded a new function.m (which works in MATLAB normally after setting the path) that this command window from MATLAB's engine is not recognizing or finding.
» isnormq(Q)
??? Undefined function or method 'isnormq'
I thought they (command window and MATLAB) were synchronized but now I see they aren't.
How can I solve this so I can use my function.m from the command window? Any help will be welcomed.
As it has been a long time and nobody answered I will post what I did:
In Matlab everytime you need a newtoolbox you link the path once and there stays "forever". You can use functions from the toolbox as if they were by default in Matlab.
Using matlab engine this doesn't work that way so it is necessary to write the command line:
% Here we load the toolbox for converting quaternion to Euler Angles
addpath(genpath('C:\Program Files (x86)\MATLAB\R2010a\toolbox\SpinCalc')); //for example
It's been a long time but I face the same problem and find some interesting information about it.
First of all at the newer version of MATLAB (2016a) MATLAB Engine search path is C:\Program Files\MATLAB\R2016a (Or the same path where you install MATLAB). So if you don't change it C++ can use all the build-in functions. Furthermore, you can using functions from Toolboxes!
But there is another one problem: what about user defined functions? So I need to put .m file directly to C:\Program Files\MATLAB\R2016a to make it visible for MATLAB Engine.
Here we can go another way - just add the path of your .m file to MATLAB through C++:
char CommandChangePath[MAX_PATH];
strcpy(CommandChangePath, "addpath('C:\\Users\\SuperUser\\Documents\\Visual Studio 2017\\Projects\\MyCppProject')");
engEvalString(ep, CommandChangePath);
For me its very useful to put necessary MATLAB function in current C++ project, add the path and use then! Now you don't need to change the path at every step - it's remembered and useful always for current application.
I have a C++ program that keeps generating data. I have a python class that process these data. I want to use this python class to process the data: when each time a data point is generated, I can use this python script to process the data. But this python script must be "stateful", i.e. it should be able to remember what it did before this data point.
One super basic example is, my C++ program just generates numbers, and my python class calculates the cumulative sums of the numbers generated:
Python:
class CumSum:
def addone(x):
self._cumsum += x;
print self._cumsum;
C++
[Somehow construct a CumSum instance, say c]
for (int i=0; i<100000; i++) {
int x = rand() % 1000;
[Call c.addone(x)]
}
I heard boost::python is a good way to handle this. Can anyone sketch out how to do it? I tried to read boost documents but they were too huge for me to digest.
I appreciate your help.
For basic information about how to execute your python script:
http://www.boost.org/doc/libs/1_47_0/libs/python/doc/tutorial/doc/html/python/embedding.html
For details on manipulating python objects in C++
http://www.boost.org/doc/libs/1_47_0/libs/python/doc/tutorial/doc/html/python/object.html
Much of boost-python is concerned with exporting your C++ classes to python but you aren't doing that so you can ignore it.
You may be better off using a simpler wrapper like SCXX
http://davidf.sjsoft.com/mirrors/mcmillan-inc/scxx.html
The following code of mine computes the confidence interval using Chi-square's 'quantile' and probability function from Boost.
I am trying to implement this function as to avoid dependency to Boost. Is there any resource where can I find such implementation?
#include <boost/math/distributions/chi_squared.hpp>
#include <boost/cstdint.hpp>
using namespace std;
using boost::math::chi_squared;
using boost::math::quantile;
vector <double> ConfidenceInterval(double x) {
vector <double> ConfInts;
// x is an estimated value in which
// we want to derive the confidence interval.
chi_squared distl(2);
chi_squared distu((x+1)*2);
double alpha = 0.90;
double lower_limit = 0;
if (x != 0) {
chi_squared distl(x*2);
lower_limit = (quantile(distl,((1-alpha)/2)))/2;
}
double upper_limit = (quantile(distu,1-((1-alpha)/2)))/2;
ConfInts.push_back(lower_limit);
ConfInts.push_back(upper_limit);
return ConfInts;
}
If you're looking for source code you can copy/paste, here are some links:
AlgLib
Koders
YMMV...
Have a look at the Gnu Scientific library. Or look in Numerical Recipes. There's also a Java version in Apache Commons Math, which should be straightforward to translate.
I am trying to implement this function as to avoid dependency to Boost.
Another option is to reduce Boost dependencies, but not avoid them. If you reduce the dependency, you might be able to use a Boost folder with say, 200 or 300 source files rather than the entire 1+ GB of material. (Yes, 200 or 300 can be accurate - its what I ended up with when copying out shared_ptr).
To reduce the dependency, use bcp (boost copy) to copy out just the files needed for chi_squared.hpp. The bad thing is, you usually have to build bcp from sources because its not distributed in the ZIPs or TARBALLs.
To find instructions on building bcp, see How to checkout latest stable Boost (not development or bleeding edge)?