Write a figure to a file automatically in MATLAB - matlab

Does anyone know if it's possible to automatically write a figure out to a .eps file in MATLAB?
I'm running a script that produces a large number of graphs, and it'd be nice if I didn't have to manually save each one!

print function does that:
Print figure or save to specific file format...
print(filename,formattype) saves the current figure to a file using the specified file format, such as print('BarPlot','-dpng'). If the file name does not include an extension, then print appends the appropriate one.
print(filename,formattype,formatoptions) specifies additional options that are available for some formats.
print prints the current figure to the default printer...

print or saveas will do the trick.
saveas(fig_handle, 'filename','eps')
print('-deps',fig_handle)
print -deps 1
If you want to specify the output file name, you're better off using saveas.

This was answered in this other question, using the PRINT command. Although that question dealt with making .tiff images, it should be straightforward to modify the code given in those answers to write a .eps.

Suppose, you are generating N numbers of figures in a loop, then you should try the command line:
saveas(gca,sprintf('Figure%02d.pdf',N )); it produces N figures Figure1.pdf - FigureN.pdf
saveas(gca,sprintf('Figure%02d.eps',N )); it produces N figures Figure1.eps - FigureN.eps
in place of gca one can use gcf also. First command line is a better solution.
Hope this will solve your issue.

Related

How to use 'fprintf' to show the output in a txt file and save it instead of command window in Matlab?

I've been trying to make the output shown in the text file instead of the command window. Im blur right now as i already look at a lot of example but it always show the error on fprintf. I try to edit the code of fprintf(fid,'%s\n',word);%Write 'word' in text file (upper) in one of the Matlab example which is Automatically Detect and Recognize Text in Natural Images.
This is the link of the code.
https://www.mathworks.com/help/vision/examples/automatically-detect-and-recognize-text-in-natural-images.html?s_tid=srchtitle
Basically the above link display the output on the command window. But, i need it to be on the txt file.
Im really new to this, i want to know what code do i need to put, how and where should i put the fprintf to make the output shown on the text file and not on the command window.
Also, can i save the text file after that? do i to put any additional code?
I really need your help. Thank u in advance!
It seems you're looking for the fopen() method. It takes two parameters, the first being the name of the file you'd like to write to, and the second being the mode. If the file specified does not exist in the root directory, it will be created on execution.
fileID = fopen('exp.txt','w');
fprintf(fileID, fid,'%s\n', word);
fclose(fileID); % Make sure to always close the stream after finishing
More on fopen() here

Exporting a figure

I came across this program for exporting a figure. I just want to ask, what should I pass to the program if I want to export a figure of mine to some image (i.e; .jpg), as I couldn't figure out what to pass as a parameter.
Thanks.
The built-in command print is used to export a figure to an image. Like so:
plot(rand(4))
print('-djpeg', 'myplot.jpg')
patrik's suggestion probably refers to this ME file. (Which is great, by the way)
A native MATLAB solution would be
saveas(<handle of the figure>,<file path with .jpg at the end>)
This is what I have used for a long time and it works (e.g., saveas(h,'/figures/automatic/myfig.jpg')); but it is very possible that print or something else is better suited for whatever reason.

Matlab script should output to file and to the console

What is the best way to save the output of a MATLAB script to a file AND output it to the console? I do not want to use diary b/c sometimes I try things out between script runs. Right now I use fprintf to output either to the console or to a log file. Can I do both without writing the same fprintf for both outputs?
I may be in the wrong direction, but suppose you want to output something to the console, and save it, here is what you could do:
s = 'abcd'; %The thing you want to show and save
s % Just show it
save s % Save it to file
If however, you need a specific output that only comes from the print command. Here is what I would do:
Print to a variable
Save that variable
Show that variable
This way you only need the print once.
If that also does not work out for you, you could try to write a function where you do the print, at least then you can avoid code duplication.

Gnuplot incremental filename using macro

Hell,
I need to plot points out of my c++ application.
So I simply save my points to a points.txt
and then run system("gnuplot 'plotmakro'");
which contains:
set output 'plot.png' set terminal png
set grid set multiplot
plot pointsa.txt' ', 'pointb.txt'
Is there a solution so that I get plot2.png, plot3.png when running the makro again?
As far as I understand your problem two possible solutions come to my mind:
sed the output of your gnuplot script to another location before running gnuplot with the newly created script or
output the png to some arbitrary file like tmp_plot.png and change the file name after gnuplot is done to your liking.
However, with both suggestions I somehow feel that there is a nicer and cleaner solution to your problem. Maybe you want to think about your interface between your application an gnuplot...

Save currently running script in Matlab

Is there a way of saving the currently running script in Matlab? I have a script which automatically takes a backup of a set of scripts although if I have changed the current script then the saved version will be out of date.
Perhaps its possible to call some java?
Thanks
Somewhere on Yair Altman's site (see link in my other answer) he also referred to a blog entry about editorservices, which was introduced with MATLAB R2009b.
editorservices.getActive().save
should do what you want.
Okay, all I write here I learned from undocumentedmatlab.com by Yair Altman, in particular by looking into his EditorMacro.m... great stuff!
I'm assuming that Itamar Katz understood you correctly and that you are running unsaved code from the editor by using "Evaluate Cell" or "Evaluate Selection"; you want your code to realize that it is not saved and save the version currently displayed in the editor to some other location.
I have not found a way to save the file directly to the original location, but at least I have found a way to access the current text. You can then use fprintf to save it to wherever you want. I have tested this in Matlab 7.11 (R2010b); if you have a different version you'd need to dig through EditorMacro.m to find the correct code for Matlab 6.
if com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.isDirty
thisdocument=com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.getDocument;
thisdocument_text=char(thisdocument.getText(0,thisdocument.getLength));
fid = fopen('backupfile.m','w');
fprintf(fid, '%s', thisdocument_text);
fclose(fid);
else
% saved file is unmodified in editor - no need to play tricks...
...
end
So the if-condition checks if the currently active editor window contains a file which is not saved ("dirty"); if it is, we need to retrieve the current version of the code (into variable thisdocument_text) and save this string to some file.
Does this help?