Matlab P file opening in Octave - matlab

I use Octave and have a Matlab made P file (protected function file). How can I open it, use it with Octave? Are there any method to convert it maybe or get content from it?

P files are protected, you can't open them, that's the whole point of them. You should still be able to run them in MATLAB, but I'm not sure if they'll work in Octave.

Related

detect whether .m file is matlab or mathematica

I have been editing Matlab script in Vim for some time now. Recently I started taking an interest in moving my Mathematica work to plain text files so they can be managed using git and edited using Vim. Unfortunately Mathematica also uses the .m extension for its package files and is therefore not easily distinguished from Matlab scripts. Since I would like my editor to do the work for me I was wondering if anyone has come up with an idea for identifying both based on the contents of the file. I would be fine with something that works in most cases, but am reluctant to use a solution that requires alterations in the scripts such as adding a comment.
A ".mat" or ".m" file created with MATLAB's save() function will always start with the plain text identifier "MATLAB". So, using MATLAB syntax, you might do this:
% Set up a workspace variable and save to file
tmp = 1:10;
save('test.m');
% Open the file, read the first line and close again
fid=fopen('test.m');
firstline=fgetl(fid);
fclose(fid);
% Branch depending on file format
if ((numel(firstline) >= 6) && strcmp(firstline(1:6), 'MATLAB'))
disp('This may well be a MATLAB file.');
else
disp('This is probably not a MATLAB file.');
end

How can I import a function of Octave to 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.

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.

Matlab: Saving plot images, override plot.m

Working on MATLAB 2008, I am trying to save all the images my scripts produce when invoking the "plot" function.
In order to achieve this, I have two possible solutions:
Either I write another function having the same parameters and perform a search/replace in the *.m sources
or I override the plot.m file so that I write the image into a specific directory when generated.
I did many searches and I am unable to find the plot.m source file. The only file I found is located in the toolbox directory and does not contain any code (except some commented documentation).
you can simply use the print command and save them into a directory that you can also make using the mkdir command.
Sample code
clc; close all; clear all;
x = 1:10;
y = x.^2;
plot(x,y)
if exist('plots','dir') ~= 7
mkdir('plots'); % make directory if it does not exist
end
print -dpdf ./plots/jawn.pdf
Read the print documentation, to learn how to print in other file formats
Also, I would not suggest overriding the plot command, and you will likely not be able to find the source code for plot.m because that is proprietary MATLAB code