Importing graphic data into text file MATLAB - matlab

I am trying to create a program in MATLAB by which I can click on an image (using the input command) and MATLAB outputs a graphic marker on the image, and stores the data to a text file.
The motivation for this comes the Major League Baseball Gameday App, as I want to use it for a similar purpose, but on the input rather than the receiving end. I have created a code which takes an image and outputs a graphic marker at the points clicked. However, I need to improve the code by (hopefully) color coding each marker according to a manual input (maybe using the reply function?) and also numbering each marker. Also, I would like it to output the reply, location, and number to a text file which can be saved for later.
The code I have is below (with comments explaining what is missing), any help is appreciated.
img=imread('Path/Filename.png');
image(img);
title('Pitching')
ginput
%ask for reply of text between each click
M=[ans];
N=length(M);
hold on
for i=1:N
plot(M(i,1),M(i,2),'o','MarkerFaceColor','r','MarkerSize',25);
%make FaceColor coded according to reply from click
%number each click
%write data to text file
end

Related

Matlab update plot in one GUI based on activity in second GUI

In one GUI (viewer) I have an image that shows a 2D slice through a 3D image cube. A toolbar button opens a second GUI (z-profile) that plots a 2D graph showing the z-profile of one pixel in the image cube. What I want is to be able to update this plot dynamically when a different pixel is clicked in the original viewer GUI. I've looked in to linkdata but I'm not sure if that can be used to link across two GUIs. Is there a simple way to do this without re-creating the second GUI each time a new pixel is clicked and feeding in the new input location?
You can definitely doing it without recreating the second GUI every time.
Without knowing your specific code I would say that you should store a reference to the second GUI in the first GUI, then in a callback for clicking a pixel in the first GUI, change data in the second GUI via the stored reference (e.g. figure handle). You can store arbitrary data in a figure, for example by using function guidata. A bit of code.
...
figure2 = figure();
figure1 = figure('WindowButtonDownFcn',#myCallback);
guidata(figure1, figure2);
...
function myCallback(obj,eventdata)
figure2 = guidata(obj);
...
Even easier but a bit more error-prone would be to use global variables for storing the references.

Saving GUI as .jpg in 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);

How to copy (to clipboard) only significant part of a plot in Matlab?

How to copy (to clipboard) only significant part of a plot in Matlab? Without that large gray margins Matlab loves so much?
Is it possible to populate clipboard from Matlab command line?
As far as programmatically populating the system clipboard with image data captured from a Matlab figure goes, you might try Yair Altman's, ScreenCatpture utility on the Matlab File Exchange:
screencapture('handle',gca,'target','clipboard');
It has a lot of options and will allow you to specify a region or an entire figure. It does require Java, i.e., it won't run in '-nojvm' mode. You can read more about it at Yair's Undocumented Matlab site.
Also, you can change the current figure's background color to white before copying it via:
set(gcf,'Color','w');
In the figure window, choose "edit" -> "Copy Figure".
It's equal to Ctrl+C on text, etc., but takes only the main part of the figure. So if you go to word, say, and press Ctrl+V, or RIght Click -> Paste, you will get only the main part of figure, without the gray margins you hate so much...

Dynamic grid of images in MATLAB

I'm working on creating a GUI in matlab using GUIDE. However, i'm not exactly sure how to do the following, and was looking for some tips/advice.
Problem
I want to open a directory and display all the images in that directory in the GUI interface when if it's selected. However, since I will never know exactly how many images there are I am not entirely sure how to do this in the GUI.
Essentially, I want to open the directory and all the images to be displayed in a grid on the GUI similar to that in iphoto.
Current code
Currently, I can open a directory fine, and get all the required information as follows:
directory = uigetdir(pwd, 'Directory Selector');
files = dir(fullfile(directory, '*.jpg'));
strcat(strcat(directory, '/') , files.name) %outputs each file's location
I'm just not sure how to translate this information into the GUI without writing numerous handles.axes1. I understand that since I know this info I could loop over them, but would I not have to create the axes to begin with?
You probably don't want to do this with individual controls - the reason is that MATLAB will have to render each and every one, which will be slow if the directory has a lot of images. Clearly, you can only display a certain number of images on screen at once. You would also have to write your own scrolling code (or some kind of pagination control).
If you have MATLAB > R2008, you can put images in uitable cells using HTML:
% Example for a control with a 'String' property
set(handles.myControl, 'String', '<html><b>Logo</b>: <img src="http://UndocumentedMatlab.com/images/logo_68x60.png"/></html>');
See also this post and this Undocumented MATLAB page.
A different option would be to use the Windows common controls ListView.
A simpler way of doing this would be to have a single image and a listbox of files; an example is here
You can add components to a GUI pprogrammatically. There's more information here.
Each new axes can be added with something like this:
ah = axes('Parent',hObject,'Position',[left bottom width height]);
where left, bottom, width and height define the size and position of the axes. You'll need to change the position for each axes you create and keep track of the axes handles.

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.