detect whether .m file is matlab or mathematica - matlab

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

Related

Writing matlab vector to a file that is matlab readable

For matlab: Is there a way to write the value of a vector to a file that can later be opened and read by another matlab program?
Specifically: I have a matlab program that computes a binary-valued vector $zvector$ with 10^7 entries. I want to write $zvector$ as data to an output file so that it can be emailed and easily read as input to another matlab program. Ideally, the output file would be called “Output.m” and would look like:
zvector=[
0
1
1
…
0
1
];
I like the .m format because it is easy to use for matlab input. I have experimented with matlab’s write() and fwrite() commands, with no success. I observe that these generate files that cannot be easily read as matlab-recognizable inputs (at least, I do not know how to read from them). Is there a way to accomplish my goals? Thanks.
PS: I am interested in the easiest way. If this involves a different type of file format (not a .m format) that is fine. However, in that case, can you provide both the writing and reading commands? Thanks again.
Thanks to #edwinksl for pointing me in the right direction with MAT files. I do not know the accepted practice here, but in stackexchange math it is encouraged to answer your own question if a hint from comments got you all the way there. So I will answer my own question.
The Mat format does this well. Here are example script files for reading and writing in the Mat format (see also links in above comments for more documentation):
***Script file OutputTest.m:
filename = 'TestFile.mat';
TestVector=[1 1 0 1];
save(filename, 'TestVector');
***Script file IntputTest.m
filename = 'TestFile.mat';
file=load(filename);
z =file.TestVector;
z

Matlab P file opening in Octave

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.

Matlab Publish Image PDF

I want to include a picture of my Simulink diagram when I publish my script. I currently print it to an image and manually add it in later.
I read on the Matlab website that I could do this by including a comment <<FILENAME.jpg>> and the publishing tool would pick it up and add it to my report. Unfortunately all this does is add a link to the image, which is not helpful when printing it out.
This is what I have:
print(['-s',simName], '-djpeg', ['html/',simName,'.jpg'])
% <<simName.jpg>>
I've also tried these tips before, but they didn't work for me either.
What you have tried should work. In fact it should work with any JPG file. have you tried what the example from the documentation?
To produce an HTML file containing surfpeaks.jpg from a MATLAB file:
Create a subfolder called html in your current folder.
Create surfpeaks.jpg by running this code in the Command Window.
saveas(surf(peaks),'html/surfpeaks.jpg');
Publish this MATLAB code to HTML.
%% Image Example
% This is a graphic:
%
% <<surfpeaks.jpg>>
%
The alternative is to open the Simulink system with open_system. This will create a snapshot of the model in the published HTML provided the model is closed when the open_system command is issued.
%% Include snapshot of f14 model
%
open_system('f14')
%
If none of these work, then there's something fundamentally wrong with your MATLAB installation, of the way you are using the publish command.
open_system('filename.slx')
make sure the simulink program is in the same folder as the script.

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

Text and Plots in Matlab to LaTeX

I like to create a "report generation" script in Matlab.
Suppose we have a Matlab array, data and we want to export the following to a .tex file:
"The information in the first element of data is X." This would be followed by a plot of X.
I have already tried help latex in Matlab and aware of the various packages on Matlab file exchange. However I have seen nothing so far that will allow me to export both text and plots in the same Matlab script to a .tex file.
The publish function may work for you.
Create this script, foo.m:
%%
% The information in the first element of data is X.
plot(X)
And publish it to LaTeX:
>> publish foo latex
You might want to take a look at this article published in TUGboat (the official magazine of the TeX Users Group):
http://www.tug.org/TUGboat/Articles/tb24-2/tb77seta.pdf
Generating LaTeX documents through Matlab (S. E. Talole and S. B. Phadke)
Good luck!
Are you aware of matlab2tikz? i've used it extensively for my PhD-Thesis, albeit only for exporting single plots. But I guess it should be easily possible to whip something up that combines the power of MATLABs LaTeX export capabilities.
Exporting figures from Matlab to a .tex file is just a matter of exporting the figure to an appropriate format and then including the figure file in the .tex file. Would something like the code listed below work for your needs?
Using LaTeX to generate dvi:
% Include code to write text to .tex file (fid is assumed to be the file id of the .tex file)
print(gcf,'figure1','-depsc','-r100');
fprintf(fid,'\includegraphics[width=4in]{figure1.eps}\n');
Using pdfTeX to generate pdf:
% Include code to write text to .tex file (fid is assumed to be the file id of the .tex file)
print(gcf,'figure1','-djpg','-r100');
fprintf(fid,'\\includegraphics[width=4in]{figure1.jpg}\n');