Matlab Publish Image PDF - matlab

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.

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

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

Is there a way of selectively including code when publishing in Matlab?

I'm writing MATLAB code in order to publish it later. By publishing, I mean the built-in MATLAB publish tool that allows the programmer to make a full report generated from their MATLAB code. There's an option to include the code with this report, section by section, preceding the results of this code. Is there a way to tell MATLAB to include some of this code in the report but not all of it? I know there are quite a few markup code tags, but I wasn't able to find anything on this topic.
Edit: Just to clarify, I want all results to be published, but only some of the code. So simply removing this code is not an option.
Cheers! = )
Hide your code that you don't want people to see in a script. For example, in the "sine_wave" example from the publish documentation page, I added a single line:
junk
Here's the content of junk:
figure()
plot(0:0.01:6,sin(0:0.01:6))
Now run your main script, and the published result has "junk" in the listing, but the contents of junk are not included, and you get the nice version of a sine wave, instead of the crappy one included in their example.
The only way I know of to do this is to remove the code that you don't want to appear in the output. If you just want to display the code and not the output, then you can just set the evalCode property to false in your call to publish.
If you do want the code to be evaluated, and the output to be published as well, then it's just slightly more complicated. You can manually execute the parts of the script that you don't want to publish, then publish the code that you care about (by putting it in it's own .m file). It shouldn't matter if the published code depends on any variables that are initialized in the omitted code, since those variables were added to your workspace when you manually executed the omitted code fragments.
Edit:
Since you've clarified your question to state that you're interested in publishing some of the code, but all of the output, I would think that your best bet is to just modify the "temporary" script (which contains the partial set of code that you wish to publish) to include any fprintf, disp, etc. function calls that you want to have appear in the output.
It's a bit hack-ish, but like I said, I'm not aware of any way to get that kind of fine granularity with "annotations" or using the publish command.
Hope that helps!
Here's a sample script that you can save and publish which will illustrate one workaround. You first have to set the Include code option to false, which stops all evaluated code from appearing, but you can still display code using a syntax highlighted code sample:
%% Controlling what code gets published
% Here's how you can do it...
%% Showing results without code
% If you set the
% <https://www.mathworks.com/help/matlab/matlab_prog/specifying-output-preferences-for-publishing.html#bthbe__-3
% *Include code* option> to |false|, you will see the plot but not the code
% that made it:
surf(peaks); % I'm John Cena!
%% But what if you want some of the code to show?
% The *Include code* setting affects the whole document, so all evaluated
% code will be hidden. If you want some code to show, you can use
% <https://www.mathworks.com/help/matlab/matlab_prog/marking-up-matlab-comments-for-publishing.html#bs_uwzr
% syntax highlighted sample code>. This does mean you have to have duplicate
% sections of code (one is evaluated, one is displayed), but it's the best
% option thus far:
%%
%
% surf(peaks);
%
surf(peaks); % You can't see me, but you see the above!
And here's the published output:
I change the Matlab expression in the publishing options to
myFunction('PUBLISHING');
And the first lines of the function code to check for that input, so I can modify my code to only do certain things when publishing, usually displaying figures etc., but not during normal operation. Or vice versa :)
function [outputs] = myFunction(input1, input2)
isPublishing = (nargin == 1) && strcmp(input1, 'PUBLISHING');
if (nargin == 0) || isPublishing
% Set up default values
input1 = 'Hello';
input2 = 'World';
end
...
end

How to export the output of a SOM in MATLAB

Ok, so this question is related to my ongoing task of getting text data categorized, you can refer to this question for more details on how I approached this problem.
I used the standard matlab function "nctool" (neural clustering tool) to get my inputs organized on a plane of 10x10 SOM nodes. I also got the output of this map (i.e. which of my inputs ended up on which node) saved in to the "output" variable in my workspace.
I would now like to get this data out and see if I can write another script. I'm aware of the 'save' and some of the export functions in MATLAB, however it seems that MATLAB does not support ascii export of this variable since it is a sparse matrix.
I am currently writing a script to get this thing exported out, however if someone already has a solution, please post. Otherwise I will do so after I finish testing it.
Update: I found a workaround to this fairly easily:
% convert a sparse matrix to full
output = full(output);
% output this to a file (excel)
xlswrite('test.csv',output);

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');