Tag coordinates and ginput - Matlab - matlab

I wish to tag/mark coordinates on an axes in App designer using this simple code but without success. Any idea how to do it directly on the axes (app.ImageAxes)?
Code:
function ButtonPushed(app, event)
imshow('cameraman.tif','Parent',app.ImageAxes);
app.ImageAxes.HandleVisibility = 'on';
[x,y,button] = ginput(5)
app.ImageAxes.HandleVisibility = 'off';
end

Related

Get currently selected data point from axes in a figure

I have a MATLAB figure with an axes containing a scatter plot.
Every point on this scatter plot has a signal data array associated with it.
I want to take user input as point selection from scatter plot and plot the corresponding signal data in another axes on the same figure.
Combine a global definition of your second axes with an UpdateFcn on the datacursor. See example below, which generates a sine wave based on a random variable selected.
function getSelectedDataPoint()
% create figure
fig = figure;
% make second axes a global to adress in myupdatefcn
global ax2
% define axes
ax1 = axes('parent',fig,'position',[0.05 0.05 0.9 0.4]);
ax2 = axes('parent',fig,'position',[0.05 0.55 0.9 0.4]);
% Random scatter
scatter(ax1,rand(25,1),rand(25,1),25,'filled')
% Set datacursormode to on
dcm_obj = datacursormode(fig);
datacursormode on
% Specify objective function for clock
set(dcm_obj,'UpdateFcn',#myupdatefcn)
% Define objective function
function text = myupdatefcn(~,obj)
text = sprintf('X: %f \n Y: %f',[obj.Position(1),obj.Position(2)]);
% Find corresponding signal
id = find(and(obj.Position(1) == obj.Target.XData,obj.Position(2) ==
obj.Target.YData));
% Do your thing with the signals
x = 0:0.1:100;
y = sin(obj.Target.XData(id)*x);
% plot on second axes
plot(ax2,x,y)
end
end
I am not sure I entirely understand what you want to do, but you might want to use the "Tag" option available for many objects in MATLAB.
Replace line under "Random scatter" by:
% Random scatter
hold(ax1,'on')
scatTag = cell(1,10);
for i = 1:10
scatTag{i} = scatter(ax1,rand(1,1),rand(1,1),25,'filled');
scatTag{i}.Tag = num2str(i);
end
In the data cursor update function, replace the line "id= find(..."
tagname = obj.Target.Tag;
And modify the signal function to point to your target tag, whatever function that is. In my example, you could do this to define the y-values:
y = sin(str2double(tagname)*x);
It will generate another sine wave, based on the tag.
Hope this helps :)

How to make more tick marks appear in graph?

I want more tick marks appear in graph. For example, if I do this: plot(1:1000), I get the following:
How to make more tick marks appear as shown at the X-axis of the following figure?
I want to do the same for Y-axis. Customizing this is not documented.
For more recent versions of MATLAB you simply grab the axes and change the YMinorTick property to 'on':
plot(1:1000);
ax = gca;
ax.YMinorTick = 'on';
For older versions, you have to grab the axes using the set function:
plot(1:1000);
set(gca, 'YMinorTick', 'on');
We get:
If you have MATLAB 2016a or later you can use the Ruler properties:
plot(1:1000);
ax = gca;
ax.YMinorTick = 'on';
ax.YAxis.MinorTickValuesMode = 'manual'; % prevents MATLAB form update it
tick_gap = ax.YAxis.TickValues(2)-ax.YAxis.TickValues(1);
minor_tick_no = 5;
minor_gap = tick_gap/minor_tick_no;
ax.YAxis.MinorTickValues = ax.YAxis.TickValues(1)+minor_gap:...
minor_gap:ax.YAxis.TickValues(end);
And the same for the ax.XAxis property.

Save axes in GUI as image MATLAB

I know there are a lot of answers regarding this issue but I didn’t found any one that help me..
I have a GUI in MATLAB with 2 axes and I want to save separately each axes as .jpeg or any other format.
Any way I have tried – I got either image that including all the GUI or cut figure.
Any idea how can I get 2 good images?
You could loop through all of the axes and call getframe to get just that axes. You can then save the cdata using imwrite.
% Get a list of all axes in the figure
allax = findall(gcf, 'type', 'axes');
for k = 1:numel(allax)
% Get the axes as an image
fr = getframe(allax(k));
% Save the image
imwrite(fr.cdata, sprintf('%d.png'));
end
If you already have axes handles you can just use those directly
fr = getframe(axes2);
imwrite(fr.cdata, 'axes2.png')
fr = getframe(axes1);
imwrite(fr.cdata, 'axes1.png')
If you want to include the X and Y axes labels, you could do something like
function axes2image(ax, filename)
hfig = ancestor(ax, 'figure');
rect = hgconvertunits(hfig, get(ax, 'OuterPosition'), ...
get(ax, 'Units'), 'pixels', get(ax, 'Parent'));
fr = getframe(hfig, rect);
imwrite(fr.cdata, filename);
end
axes2image(axes2, 'axes2.png')
axes2image(axes1, 'axes1.png')

Include axes labels when saving plot from MATLAB GUI

I have written the following code to try and retrieve ONLY the axes and its plot from my MATLAB GUI.
F = getframe(gca);
figure();
image(F.cdata);
saveas(gcf,'PlotPic','png');
close(gcf);
I noticed, however, that this method does not include ANY of my axis labels or title. Is there any way which I can get the getframe function to include the axis labels and title?
I tried the following code but it did exactly the same
pl = plot(x,y);
xlabel('x')
ylabel('y')
ftmp = figure;
atmp = axes;
copyobj(pl,atmp);
saveas(ftmp,'PlotPic.png');
delete(ftmp);
I would do it using the rect option of the getframe function.
Basically you can provide a 2nd input argument to getframe, which then captures the content of the rectangle specified as argument. The nice thing is that you can use the handles to an axes, so it does not capture your whole GUI figure but rather a specific axes.
For example, using this line:
F = getframe(gca,RectanglePosition);
Concretely, you could set the coordinates of the rectangle such that they span both axis labels and the title as well. Here is a sample code. The pushbutton callback executes getframe and opens a new figure with the content of F.cdata:
function GUI_GetFrame
clc
clear
close all
%// Create GUI components
hFigure = figure('Position',[100 100 500 500],'Units','Pixels');
handles.axes1 = axes('Units','Pixels','Position',[60,90,400,300]);
handles.Button = uicontrol('Style','Push','Position',[200 470 60 20],'String','Get frame','Callback',#(s,e) GetFrameCallback);
%// Just create a dummy plot to illustrate
handles.Period = 2*pi;
handles.Frequency = 1/handles.Period;
handles.x = 0:pi/10:2*pi;
handles.y = rand(1)*sin(handles.Period.*handles.x);
plot(handles.x,handles.y,'Parent',handles.axes1)
title('This is a nice title','FontSize',18);
guidata(hFigure,handles); %// Save handles structure of GUI.
function GetFrameCallback(~,~)
handles = guidata(hFigure);
%// Get the position of the axes you are interested in. The 3rd and
%// 4th coordinates are useful (width and height).
AxesPos = get(handles.axes1,'Position');
%// Call getframe with a custom rectangle size.You might need to change this.
F = getframe(gca,[-30 -30 AxesPos(3)+50 AxesPos(4)+80]);
%// Just to display the result
figure()
imshow(F.cdata)
end
end
The GUI looks like this:
And once I press the pushbutton, this is the figure that pops up:
So the only trouble you have is to figure out the dimensions of the rectangle you need to select to capture the axis labels and the title.
Hope that solves your problem!

Drawing multiple plots to one figure frame and zooming with mouse click

I want to put multiple plots to one figure windows and when I click one of these it will be open on separate window. Is there any predefined function to do that or what is the trick that makes possible?
Yes you can to this, you need to define a callback-function that does what you want and then set the axes property ButtonDownFcn equal to this callback.
a(1) = subplot(311); // plot stuff
a(2) = subplot(312); // plot stuff
a(3) = subplot(313); // plot stuff
set(a,'ButtonDownFcn', #copyAxesToNewFigure);
With regards to creating a new Figure that contains a copy of the axes you clicked on, a function like this should work:
function copyAxesToNewFigure(hObject,eventdata)
childHandle = get(hObject, 'Children');
newFig = Figure;
newAx = Axes;
copyojb(childHandle, newAx);