Saving GUI as .jpg in matlab - matlab

In matlab, I have created a GUI with several plots and edit boxes filled with text for an experiment with the bike. These plots and edit box values vary depending on the data I input (120 different sets). I was able to save the GUI as a figure with
saveas(handles.speedbike,fullfile(savename), 'fig');
Speedbike is the handle I gave to one of the plots and savename is the name that the figure is saved under, which changes with each set.
Now I also wish to save all the sepparate GUI's as jpegs, but using the same code as above, but with 'jpg' instead of 'fig' only saves a small corner of the figure as a jpeg, not the whole GUI.
Is there any function that I can use to correctly save a gui as a jpeg, or any way to open a .fig file, and then saving a copy of that as a jpeg.?

You need to use the handle of the whole figure for your GUI, instead of the handle of one of the plots. You probably also want to use print for saving to jpg.

Ive found satisfactory results with this:
hg=get(0,'Children');
saveas(hg,'.\gui.fig','fig');
optsave.Format='png';
hgexport(hg,'.\gui.png',optsave);
Why this do not work?. This normally do with normal figures, and failed with GUI's:
optsave.Format='meta';
hgexport(hg,'.\gui.wmf',optsave);

Related

how to save figure of gui plot which will run on a computer that does not have matlab

I have made a Gui program that I compile to EXE application which lots csv file into graph data. I buit a save button but I do not know how save figure with different name each time cause (savefig anduisave both uses matlab program). I am posting my code below if anyoff you guys figure out how to save gui figue into image or anything that does require matlab to open. Last function is the callback function for save button.
function ma_Callback(hObject, eventdata, handles)
% i tried uisave but not possible to run computer without matlab cause mcr
% does not run uisave
% i tried copyopbj but since i did not put a name on my figure it did not
% work
%savefig
If you are trying to save a kind of image from your figure, then the best option which supports many aspects, is using print function.
I have already done this in a compiled app and it works perfect. Using print function you can set different file type(vector formats like *.svg are also supported), resolution(dpi), and so many others.
Although you can use print function directly on the figure, but I found that the best way (More Customization like removing or adding some objects and changing many options) is to follow this steps:
Create another figure with Visible='on' but a position that is out of screen with the same width and height of Main Figure.
normalized position = [-1, -1, ?, ?]. (Create this figure in start up and don't destroy it until your app exits, let call it Print Figure).
Copy your figure content (or the parts you are interested in) using copyobj to that figure (you may need to set Parent property of some key objects like panels to this new figure). They would look exactly as they look in the main figure, because they have the same properties. you can add or remove some objects in this step.
Change aspect ratio of "Print Figure" for a better output.
Print this figure with all options (file format, dpi, ...) you need. in my own GUI i allowed the user to change this settings with an input dialog.
In my app i used functions like winopen to show the output, and output directory to the user, when the print task was done. Print process takes some time, specially if dpi is huge, so it is also a good idea to inactivate buttons and show wait cursor.
Update:
a simple usage would be:
print(MainFigure, 'myFileName', '-dpng', '-r300')

Matlab figure saved as .eps appears in 'horizontal sections' when opened in Adobe Illustrator

I normally save my matlab figures as .eps and then make them better looking using Adobe Illustrator. This works for most figures but not all.
For example I plotted my data using the violin.m function from file exchange. When I save it as .eps and open it in Illustrator, I don't get an editable figure as I usually do. Instead, My figure appears chopped in horizontal sections, and all I can do is delete them (like in the figure below).
Is there anything I can do in either matlab or Illustrator to be able to edit the figure?
I don't need an .eps file, I just need to be able to edit it.
EDITS:
I tried #MattSchmatt's suggestion of using the print2eps function but I had the same problem.
Saving as .pdf doesn't solve the problem, because the image I get is not editable in Illustrator (plus, I also get the horizontal 'chunks').
A minimal, complete and verifiable example requires matlab, the violin function linked above and illustrator. But if it helps, here's the matlab code to produce a similar figure. I save by clicking on Figure -> save as. (But as I said above I tried the print2eps function and that was the same).
X = rand(100,6);
figure; violin(X)
I tried the following, didn't work either.
set(gcf, 'Renderer', 'painters')
As the author of the question suggests, I also wasn't able to export an editable (vectorized) .eps file into Illustrator. However, exporting it as .pdf does the trick. The 'fill' for the violin plots is weird, though, and has horizontal sections (perhaps something to do with the way the density estimate is being plotted?) all over. I was able to fix this and make the plots normal (with solid filled shades): select all the horizontal sections/chunks using the magic wand tool, and then increase opacity to 100% (it is set to 50 based on the exported file). Once all the required edits are made, export the file as .tiff and it seems to look fine. Hope this replicates, and thus helps! (my MATLAB version is 2016, and Illustrator version is the CS5)

Save figures EPS from Matlab P file without display

My function calls code from other functions I did not write those but I know what they do. One of them is .p Matlab file with obfuscated content.
I am doing batch processing of a number of files. I want to write the figures directly to file without display. So I can go through them separately.
Any ideas on how to achieve this.
Thanks!
Script to save matlab figures to a specified directory
The above link works but I still want to avoid displaying the figures at all. Directly print to file. But the work around can simply close all open figures after that loop.
In your script, as soon as you create the figure, set its visibility to off.
For example:
figure(28732);
set(28732,'visible','off'); %Now the figure is not shown
You can now work with the figure, plot, save etc, without the visual clutter or system overhead of displaying it.
If you want ALL of your figures to start without visibility, you can set the default property, as follows:
set(0,'DefaultFigureVisible','off').
This will cause all generated figures to be generated without visibility. (Note, this will be very confusing is you forget that this property is set.)
You should still close the figure as soon as possible in the script, as a part of good memory management.

How to edit property of figure saved in .fig file without displaying it

I want to edit a certain property of MATLAB figures saved as .fig (MATLAB's default format) files.
I create a lot of graphics-intensive figures in a script, so I choose not to display them by making the default figure invisible with set(0,'DefaultFigureVisible','off'). This sets the 'Visible' property of any new figure to 'off'. This way I can create, edit, save, etc., figures without the need to draw them, which can be taxing on CPU, GPU and their memories. I save the figures as .fig files using the saveas(handle,'filename.fig') command. This also saves the 'Visible' property, which is a problem when I do want to open the figure (e.g. by double-clicking the file in my Windows Explorer). It loads the figure, but it doesn't display it because its 'Visible' property is set to 'off'.
I want all .fig files to be saved with the property set to 'on', but how can I achieve this without displaying (= taxing) the figures? The moment I use set(handle,'Visible','on'), the figure is drawn.
So basically, I want to edit the file on a lower level than when it's loaded as a figure in MATLAB.
I think it could be done as follows, but I do not know exactly how to achieve it. One can load the .fig's data as if it were a .mat file using s=load('filename.fig','-mat');. This loads a structure s containing some fields that contain all the figure's data, properties, etc. Now, the figure handle must be found in this unkown structure and the 'Visible' property that goes along with the handle edited.
Can this be done without the figure being drawn?
I tried, but haven't succeeded, using fopen, fread and their friends.
Does anybody know how to do what I want to do?
I base my solution on the thread from the url posted by user4506754: http://www.mathworks.com/matlabcentral/newsreader/view_thread/306249
There, Jesse Hopkins posts (post 15) that you can edit a property 'ResizeFcn' to execute a function when MATLAB creates a figure. This doesn't work on my MATLAB installation, but lead me to look into the different functions you can attach to a figure in its properties. This page documents all figure properties: http://mathworks.com/help/matlab/ref/figure-properties.html. There I found the 'CreateFcn' property. Its description contains:
This property specifies a callback function to execute when MATLAB creates the figure. MATLAB initializes all figure property values before executing the CreateFcn callback.
It means that the figure is loaded with its properties, including the 'Visible' property being 'off' and then 'CreateFcn' is called.
Setting 'CreateFcn' to make the figure visible then solves my problem.
set(gcf,'CreateFcn','set(gcf,''Visible'',''on'')')
An example:
ezplot(#sin) % draw a simple figure containing a sine wave, title, etc.
set(gcf,'Visible','off','CreateFcn','set(gcf,''Visible'',''on'')' % this disables the figure and set the 'CreateFcn' property simultaneously
saveas(gcf,'sin.fig') % save the figure in the current folder as a .fig file
close % closes current figure
Now go to the current folder in your Explorer and double-click the sin.fig file. It makes MATLAB load it and, poof, the figure is drawn.
Solution found.
This doesn't edit the .fig file, as I originally asked (as a solution), but it is an alternative solution to the original problem. Now I can create and save figures without them being visible, but draw the figures the moment they're loaded by MATLAB.

image save in matlab

I have an image and after drawing some features(ellipses and text) on it I want to save it as JPEG.
h= figure(1);
imagesc(im_name);
colormap('gray');
hold on
for i=1:no_of_points;
//draw features and write some text
end
hold off
imsave (h);
I am getting a figure with features drawn on it but when I save it, it is an image (which is my orignal image 'im_name') without new features on it.
I tried also
.
.
.
imsave (h);
hold off
Thanx in advance for your help.
Maybe you should try the function saveas
saveas
Save figure or Simulink block diagram using specified format
Alternatives
Use File > Save As on the figure window menu to access the Save As
dialog, in which you can select a graphics format. For details, see
Exporting in a Specific Graphics Format in the MATLAB Graphics
documentation. Sizes of files written to image formats by this GUI and
by saveas can differ due to disparate resolution settings. Syntax
saveas(h,'filename.ext') saveas(h,'filename','format')
Description
saveas(h,'filename.ext') saves the figure or Simulink block diagram
with the handle h to the file filename.ext. The format of the file is
determined by the extension, ext. Allowable values for ext are listed
in this table.
You can pass the handle of any Handle Graphics object to saveas, which
then saves the parent figure to the object you specified should h not
be a figure handle. This means that saveas cannot save a subplot
without also saving all subplots in its parent figure.
When using the saveas function the resolution isn't as good as when manually saving the figure with File-->Save As..., It's more recommended to use hgexport instead, as follows:
hgexport(gcf, 'figure1.jpg', hgexport('factorystyle'), 'Format', 'jpeg');
This will do exactly as manually saving the figure.
Source.