Hooking AMPL with Matlab using amplfunc and spamfunc - matlab

I'm interested in hooking up AMPL with Matlab to obtain objective values, gradients, and Jacobians to test ideas for optimization algorithms on a wide set of problems. I'm aware that I can use amplfunc.mex or spamfunc.mex to do this; however, I'm having trouble compiling the .mex files using the old netlib files on Windows. The link also given in a previous question:
Using MATLAB with AMPL
is outdated. Does anyone know where I can obtain these .mex files or offer any alternative solutions? Thanks in advance.

The mex files amplfunc.c and spamfunc.c are still available at http://www.ampl.com/netlib/ampl/solvers/examples. We aren't aware of a reason why these wouldn't compile, but if you are having trouble you could write to the AMPL user forum, ampl#googlegroups.com. Be sure to explain which function you are using and to give the complete text of any error messages you see.

Related

Alternatives to extrinsic functions such as imread and other functions during code generation in MATLAB

As you may know, extrinsic functions are not outputted during the code generation process. Are there alternatives to these functions and/or solutions to this problem? My code generation error report is shown below :
Code Generation Error Report
I am surprised that I can't output size and rgb2gray either. Since these are essential to my program, I cannot avoid them.
Help will be much appreciated!
This is a good question, and I see similar questions fairly frequently. As I started using MATLAB Coder, one of the biggest pitfalls was the constant search for supported functions. I sympathize with your frustration, and I have a few tips, having been through this.
First, to your direct question, while imread isn't supported by Coder, size and rgb2gray are. Probably Coder is complaining about these because they have been passed mxArrays from the call to imread, which is fine when it is extrinsic, but not ok for separate generation. That's just a guess. A very useful tool in writing code is the list of Coder supported functions: List of Functions supported in MATLAB Coder
But even with those two, to replace imread is not a tiny task. You'll have to find another library that supports the particular file you're working with, and then stitch that in using coder.ceval. Alternatively, if you can find a pure MATLAB implementation of it, that might help.
Are you targeting a pure C library or a MEX file? If you intend to use this code within the MATLAB environment, you can always use imread separately and then pass the data.
And now to some more general observations: MATLAB Coder isn't a perfect MATLAB to C translation system. It's extremely powerful, and I've been able to write some very large projects with it, but if what you want is the ability to run any MATLAB code without MATLAB around, you should look at MATLAB Compiler, a different add-on. There's a very good Q and A about this here: MATLAB Compiler vs MATLAB Coder
When writing projects in MATLAB Coder, it's really best to start from scratch, knowing you're targeting C code ultimately. There are so many gotchas in the conversion from MATLAB to C that you have to be always vigilant while writing the MATLAB code.
One tool that helps is to right-click on a file in the "Current Folder" list that usually resides on the left-side of the main window, and select "Check Code Generation Readiness." You'll get a great report of potential problems in the file. I recommend using this often.
Another useful tool is to always put the %#codegen tag into your code. This alerts the MATLAB editor that the .m file is intended for code generation, so it provides extra context-sensitive information while you're writing the file. This helps enormously.
The most commonly missing functions for code generation are file IO functions. There are some good reasons for this, but it's frustrating nonetheless.
When you stitch in external C code, you use the coder.ceval function, which can provide excellent access to external libraries. Using this well is a whole other topic, outside the scope of this question.
If you can indicate exactly what kind of files you're interested in reading (PNG, BMP, TIFF, etc.) perhaps someone may be able to identify a good external library for you to use.

How can I use matlab source code in labview without matlab?

I found mathscript can do this,but it seem too slow.
So I want to use matlab to create a dll file used by labview,but all information I can find just how dll could used in C++,but C++ also must use the .lib file ,I am concerned labview could not use lib. file.
So can I use the .dll in labvew,and how?Is there anyother way to use matlab source code in labview without matlab?
You will need to purchase the MATLAB Compiler which is quite expensive unless you are a student.
You can also call the MATLAB application (if it's installed) using the MATLAB Script Node (which is different than Mathscript) - see here
Another alternative is Python's Numpy which I personally picked up quickly coming from a MATLAB background. If you want to go down that route I can give you further advice on how to integrate with LabVIEW.

How to convert m file to c file

I have .m file and I really need to convert it to c language. is there any compiler available to do so?
I do really appreciate your helps.
The only way I know of is using Matlab Coder, which is a quite expensive.
If you do not want to spend that money, I fear your best option is to recode the m-file in C. Does it really have to be C or might C++ be enough. Using the eigen library, Matlab code neatly transforms to C++ without to much trouble.
There is the Simulink Coder, from Mathworks, but you must purchase it. You can get a trial version for free, though.
See this similar SO question for more details.

Estimating effort to port code from Matlab to Octave

I just read a fascinating paper: http://www.psy.cmu.edu/~ckemp/Papers/kempt08.pdf
In my opinion it takes the whole area of machine learning to a completely new level because it flexibly discovers the structure of data (and doesn't only try to find a best fit for an existing structure).
The code is also available: http://www.psy.cmu.edu/~ckemp/code/formdiscovery.html
I tried to do a few experiments of my own - but unfortunately I don't possess Matlab. I tried it with Octave but it only produced all kinds of error messages, which I don't understand (I am no expert on these programs).
Could anybody perhaps have a quick look if these problems can be solved easily (or at all)? Perhaps the solution will be an easy one (this is my hope after all).
This would really be a big help! I am very much looking forward to trying a few data sets of my own.
I didn't look into the source code, but if you are going to convert it, these links might help:
Porting programs from Matlab to Octave
Differences between Octave and MATLAB
I was recently looking for an answer to the same question and found this old post. Just to add my two cents for anyone else looking...
There is an Octave library called Missing Function Library that "Finds functions that are in Matlab but not in Octave". Also, there are a bunch of great packages (read "Toolboxes") for Octave as well on SourceForge. Hope this helps!

running old mex file on new matlab releases

I'm trying to run a program originally tested on Matlab 6.5 on a new release (R2009a)
The program uses some mex files, and I get the following error when trying to run it:
??? Invalid MEX-file '/normalizedCut/common_files/sparsifyc.mexglx':
normalizedCut/common_files/sparsifyc.mexglx: symbol mxGetIr, version
libmx.INTERNAL not defined in file libmx.so with link time reference.
(the code I'm trying to tun is Normalized cut by Shi & Malic, and can be found here:
http://www.cis.upenn.edu/~jshi/software/files/NcutClustering_7.zip)
If I try to run the code on the same system but Matlab 2007a it runs ok.
Is there some problem with backwards compatibility for 2009a?
Are there any flags somewhere in the system I can change to help it work?
When I googled it I saw some references to the LD_LIBRARY_PATH env variable, but what exactly should be added to it I could not find out.
Thanks,
Yair
The source code for those mex functions appears to be available in the "Image segmentation with normalized cuts" source on this page: http://www.cis.upenn.edu/~jshi/software/ (in the specific_NcutImage_files subdirectory in the unpacked .zip)
It's pretty common for there to be problems running mex functions with different versions of Matlab. The errors you're getting look like they're due to API changes in Matlab (though that surprises me a little). I've had the most trouble because of binary incompatibilities induced by changes in gcc. I'd suggest contacting Jiambo and asking him if he can build a new version or release the source.
Worst case, you could try re-implementing those mex functions. The normalized cut algorithm is pretty straightforward in Matlab (see the Shi and Malik paper). By the names of the mex functions, they look like they're mostly duplicating existing matlab functionality (matrix multiplication, matrix sparsification). There's a non-zero chance that if you re-implemented them as regular m-code functions they'd be faster anyway due to the multicore support that's been added to Matlab.