Pushbutton zoom in and zoom out in GUI - matlab

I need code to pushbuttons "zoom in", "zoom out" for my image. Trying to used this but wrong. Please, help me. I work with the MATLAB Gui.
function btnZoomIn_Callback(hObject, eventdata, handles)
uicontrol('Style','pushbutton','String','ZoomIn','Units','pixels',...
'Position',[90 10 60 20],'Enable','off',...
'Tag','btnZoomIn','Callback',#btnZoomIn_Callback);
h = guihandles(hObject);
set(h.btnZoomOut,'Enable','on')
data = guidata(hObject);
data.magnif = data.magnif+1;
guidata(hObject, data)
function btnZoomOut_Callback(hObject, eventdata, handles)
uicontrol('Style','pushbutton','String','Zoom Out','Units','pixels',...
'Position',[160 10 60 20],'Enable','off',...
'Tag','btnZoomOut','Callback',#btnZoomOut_Callback);
h = guihandles(hObject);
data = guidata(hObject);
if data.magnif > 1
data.magnif = data.magnif-1;
if data.magnif == 1

If you want to be cheeky about it (hopefully that idiom translates...), you could just point your professor to the built-in zoom buttons.
A (non-GUIDE) example:
f = figure;
ax = axes('Parent', f, 'Units', 'Normalized', 'Position', [0.1 0.18 0.8 0.8]);
A = imread('ngc6543a.jpg'); % Read a built-in image as a sample
image(A, 'Parent', ax);
However, if you need a serious answer, see MATLAB's zoom function, which you can add to your button callbacks.
To expand on the above example:
f = figure;
ax = axes('Parent', f, 'Units', 'Normalized', 'Position', [0.1 0.18 0.8 0.8]);
A = imread('ngc6543a.jpg'); % Read a built-in image as a sample
image(A, 'Parent', ax);
zoomonbutton = uicontrol('Parent', f, ...
'Style', 'pushbutton', ...
'Units', 'Normalized', ...
'Position', [0.1 0.02 0.4 0.1], ...
'String', 'Zoom On', ...
'Callback', 'zoom on' ...
);
zoomoffbutton = uicontrol('Parent', f, ...
'Style', 'pushbutton', ...
'Units', 'Normalized', ...
'Position', [0.5 0.02 0.4 0.1], ...
'String', 'Zoom Off', ...
'Callback', 'zoom off' ...
);
Where pushing the 'on' button turns on Interactive Zooming. From the documentation:
zoom on turns on interactive zooming. When interactive zooming is
enabled in a figure, pressing a mouse button while your cursor is
within an axes zooms into the point or out from the point beneath the
mouse. Zooming changes the axes limits. When using zoom mode, you
Zoom in by positioning the mouse cursor where you want the center of the plot to be and either
Press the mouse button or
Rotate the mouse scroll wheel away from you (upward).
Zoom out by positioning the mouse cursor where you want the center of the plot to be and either
Simultaneously press Shift and the mouse button, or
Rotate the mouse scroll wheel toward you (downward).
And pushing the 'off' button turns off this interactive mode.
Hopefully this helps you in the right direction. I would recommend you investigate MATLAB's documentation, it's very comprehensive and has many examples.

Related

Share information between two (app disigner) apps

Can anyone please tell me how can i make my main app open a secondary app which will capture some values and then send them back to my main app?
I'm aware that this issue is tackled in app designer documentation, but I have been unable to implement those steps successfully. Also, I tried to run the example but Matlab says the file doesn't exists. If anyone could please share that example it would be also very helpful.
I have never tried to implement this on my own, but I often wandered myself how I could accomplish this if facing a complex apps architecture.
Actually, if you instantiate two GUIs in the same script/function, or if you have one GUI creating another GUI inside one of its functions, the simplest way would be to play with function handles. For example, the first GUI can pass a function handle defined among its functions to the target GUI's constructor and, this way, the target GUI can invoke it in order to modify the first GUI's data and/or properties when necessary.
The standard approach, anyway, which is considered as a best practice, works as follows. Let's assume that you have two GUIs named G1 and G2 and that they are distinct (you are not running two instances of the same GUI). If they are both visible (HandleVisibility set to on) and they both have a Tag identifier defined (G1 and G2 in our example), you can search for them within the Matlab "workspace". Hence:
% This is a G2 event handler
function pushbutton1_Callback(hObject, eventdata, handles)
g1_h = findobj('Tag','G1');
if (~isempty(g1_h))
% get all data associated to G1
g1_data = guidata(g1_h);
% modify a G2 object based on a G1 object
set(handles.MyTextBox,'String',get(g1_data.MyEditBox,'String'));
end
end
MATLAB's App Designer generates class-based GUI's rather than GUIDE's function-based GUIs. The advantage of this approach is that we can pass the GUIs around as objects rather than having to get creative with things like function returns or searching for objects by tag.
Here's a simple programmatic example that illustrates one approach to this concept. The main figure window opens a secondary prompt window, which provides two inputs. When the prompt window is closed, the primary GUI prints the input values to the command window and exits.
The main window:
classdef mainwindow < handle
properties
mainfig
butt
end
methods
function [self] = mainwindow()
% Build a GUI
self.mainfig = figure('Name', 'MainWindow', 'Numbertitle', 'off', ...
'MenuBar', 'none', 'ToolBar', 'none');
self.butt = uicontrol('Parent', self.mainfig, 'Style', 'Pushbutton', ...
'Units', 'Normalized', 'Position', [0.1 0.1 0.8 0.8], ...
'String', 'Push Me', 'Callback', #(h,e) self.buttoncallback);
end
function buttoncallback(self)
tmpwindow = subwindow(); % Open popupwindow
uiwait(tmpwindow.mainfig); % Wait for popup window to be closed
fprintf('Parameter 1: %u\nParameter 2: %u\n', tmpwindow.parameter1, tmpwindow.parameter2);
close(self.mainfig);
end
end
end
The sub window:
classdef subwindow < handle
properties
mainfig
label1
box1
label2
box2
closebutton
parameter1
parameter2
end
methods
function [self] = subwindow()
% Build a GUI
self.mainfig = figure('Name', 'SubWindow', 'Numbertitle', 'off', ...
'MenuBar', 'none', 'ToolBar', 'none');
self.label1 = uicontrol('Parent', self.mainfig, 'Style', 'text', ...
'Units', 'Normalized', 'Position', [0.4 0.7 0.2 0.05], ...
'String', 'Parameter 1');
self.box1 = uicontrol('Parent', self.mainfig, 'Style', 'edit', ...
'Units', 'Normalized', 'Position', [0.4 0.6 0.2 0.1], ...
'String', '10');
self.label2 = uicontrol('Parent', self.mainfig, 'Style', 'text', ...
'Units', 'Normalized', 'Position', [0.4 0.4 0.2 0.05], ...
'String', 'Parameter 2');
self.box2 = uicontrol('Parent', self.mainfig, 'Style', 'edit', ...
'Units', 'Normalized', 'Position', [0.4 0.3 0.2 0.1], ...
'String', '10');
self.closebutton = uicontrol('Parent', self.mainfig, 'Style', 'Pushbutton', ...
'Units', 'Normalized', 'Position', [0.4 0.1 0.2 0.1], ...
'String', 'Close Window', 'Callback', #(h,e) self.closewindow);
end
function closewindow(self)
% Drop our input parameters into this window's properties
self.parameter1 = str2double(self.box1.String);
self.parameter2 = str2double(self.box2.String);
% Close the window
close(self.mainfig);
end
end
end

How To Use Matlab GUI Slider Trough

I am trying to slide through images that I loaded into the GUI. When images are loaded into the GUI, I updated the slider parameters like this.
part of the "function" for image loading
if handles.nImages > 1
set(handles.frameSlider,'Min',1,'Max',handles.nImages,'Value',1)
handles.sliderStep = [1 1]/(handles.nImages - 1);
set(handles.frameSlider,'SliderStep',handles.sliderStep)
end
Then trying to slide through images and the slider arrow keys work fine but the pulling the slider trough doesn't work when I did this. When I pull the slider trough, the pull is smooth without any step-incremented sensations. It is giving me this error: Subscript indices must either be real positive integers or logicals. I think this is happening because when I pull the trough, I am setting it at values between the allowable slider increments since the pull is not step-incremented.
part of the "function" for slider pulling
sliderPosition = get(handles.frameSlider,'Value');
imagesc(handles.imageListPhs{indexes})
What could be the error?
The step size of the slider only governs how it behaves when the user clicks the arrow buttons or inside the slider trough. The location of the thumb when the user drags it is not governed by the step size so it will most likely return a non-integer, which cannot be used as an index. You will need to use a rounding function, like round, ceil, floor, or fix to convert the slider value into one that is valid for indexing.
Consider the following example:
function testcode
nA = 15;
myfig = figure('MenuBar', 'none', 'ToolBar', 'none', 'NumberTitle', 'off');
lbl(1) = uicontrol('Parent', myfig, 'Style', 'text', ...
'Units', 'Normalized', 'Position', [0.1 0.7 0.8 0.2], ...
'FontSize', 24, 'String', 'Selected Value:');
lbl(2) = uicontrol('Parent', myfig, 'Style', 'text', ...
'Units', 'Normalized', 'Position', [0.1 0.4 0.8 0.2], ...
'FontSize', 24, 'String', 'Rounded Value:');
uicontrol('Parent', myfig, 'Style', 'Slider', ...
'Units', 'Normalized', 'Position', [0.1 0.1 0.8 0.2], ...
'Min', 1, 'Max', nA, 'SliderStep', [1 1]/(nA - 1), 'Value', 1, ...
'Callback', {#clbk, lbl});
end
function clbk(hObject, ~, lbl)
slider_value = get(hObject, 'Value');
slider_value_rnd = round(slider_value);
set(lbl(1), 'String', sprintf('Selected Value: %.2f\n Can I Index with this? %s', ...
slider_value, canIindexwiththis(slider_value)));
set(lbl(2), 'String', sprintf('Rounded Value: %.2f\n Can I Index with this? %s', ...
slider_value_rnd, canIindexwiththis(slider_value_rnd)));
set(hObject, 'Value', slider_value_rnd); % Snap slider to correct position
end
function [yesno] = canIindexwiththis(val)
try
A(val) = 0;
catch
yesno = 'No!';
return
end
yesno = 'Yes!';
end
which illustrates the process:

Using editable boxes in a GUI to load part of an image in MATLAB

I have a displayable image which I load via uigetfile. I want to allow the user to choose which portion of the image he wants to load by keying in the pixel coordinates of the top-left pixel and the bottom-right pixel into editable boxes. The problem is that I'm having some serious issues with the handles structure used to store data and don't quite understand how to use it.
Here is my code. I can easily load the 4 pixels in the topleft corner of the image (that's the default setting), but I fail to load anything else when the editable box values are changed. Is there something I'm missing here?
function mygui
%%
%Initialise GUI and set up editable boxes and push buttons
f = figure('Visible', 'off', 'Position', [360 500 450 285]);
handles.data.topleft1 = 1; %x-axis position of topleft pixel
handles.data.topleft2 = 1; %y-axis position of topleft pixel
handles.data.botright1 = 2; %x-axis position of bottom right pixel
handles.data.botright2 = 2; %y-axis position of bottom right pixel
hloader = uicontrol('Style', 'pushbutton', 'String', 'Load File', 'Position', [8 5 50 20], 'Callback', {#loadbutton_Callback, handles});
htopleft1 = uicontrol('Style', 'edit', 'String', handles.data.topleft1, 'Position', [25 40 15 10], 'Callback', {#topleft1_Callback, handles});
htopleft2 = uicontrol('Style', 'edit', 'String', handles.data.topleft2, 'Position', [40 40 15 10], 'Callback', {#topleft2_Callback, handles});
hbotright1 = uicontrol('Style', 'edit', 'String', handles.data.botright1, 'Position', [25 30 15 10], 'Callback', {#botright1_Callback, handles});
hbotright2 = uicontrol('Style', 'edit', 'String', handles.data.botright2, 'Position', [40 30 15 10], 'Callback', {#botright2_Callback, handles});
set([f, hloader, htopleft1, htopleft2, hbotright1, hbotright2], 'Units', 'normalized');
movegui(f, 'center')
set(f, 'Visible', 'on', 'toolbar', 'figure');
%%
%Loader pushbutton
function loadbutton_Callback(source, eventdata, handles)
[filename, pathname, filterindex] = uigetfile('*.jpg'); %Choose mario picture here from the directory you downloaded it from
picture = imread(strcat(pathname,filename));
topleft1 = handles.data.topleft1;
topleft2 = handles.data.topleft2;
botright1 = handles.data.botright1;
botright2 = handles.data.botright2;
picture = picture([topleft1:botright1], [topleft2:botright2], :); %Trim picture dimensions according to editable box inputs
imagesc(picture)
end
%%
%Editable boxes
function topleft1_Callback(source, eventdata, handles)
%Get new input from editable box; Save it into guidata handles structure thingy
topleft1 = str2double(get(source, 'String'));
handles.data.topleft1 = topleft1;
guidata(source, handles)
end
%(Repeat 3 more times for topleft2, botright1 and botright2)
end
And as usual, here's the picture which I'm trying to trim:
(source: gawkerassets.com)
I can suggest a solution with some changes that might be not as efficient, bu they'll work. I would do this kind of passing data between callbacks simply using the fact that Your whole GUI is a nested function, so all the callbacks can acces handles without even running the guidata function:
Do achieve this just change the way boxes are calling their callbacks from
(... 'Callback', {#topleft1_Callback, handles})
to:
(... 'Callback', #topleft1_Callback)
Now adjust arguments taken by Your callbacks, so the don't take three but two:
function myCallback(source,eventdata)
although none of those will be used, so You could simply write:
function myCallback(~,~)
as You MATlab will probably suggest. And You don't need the
guidata(source, handles);
line in any of Your callbacks anymore, since handles can be accesed anyway just by its name.

How to set the uipanel size in matlab and add slider in the uipanel?

I Wanna display the output messeges in the uipanel in matlab GUI.
and if i use this code,
hp1 = uipanel('Title','UI Panel 1',...
'Position', [157.6 30.308 62.4 12.615]);
uicontrol(...
'Parent', hp1,...
'Style','text',...
'Units', 'Normalized', 'Position', [0 0 1 1],...
'String', psancitra1);
the size and the position of the uipanel is so big and located in the center of window.
How to set the position, the size(width,long) of the uipanel?
and how to add a slider in the uipanel, so if the messeges line (psancitra1) is more than the size of the uipanel, we can use the scoll bar?
Thank you for the help before :D
How to set the position, the size(width,long) of the uipanel?
With 'Position', [left bottom width height] you define where your uicontrol is placed. You should also check the parameter Units.
For example like this:
h=figure;
hp1 = uipanel('Parent', h,'Units','Normalized','Title','UI Panel 1',...
'Position', [0 0 .5 0.3]);
uicontrol('Parent', hp1,'Style','text',...
'Units', 'Normalized', 'Position', [0 0 0.5 1],...
'String', 'psancitra1');
You can also check the documentation at mathworks:
matlab: uicontrol properties-> position

Open winvideo stream with push of a button in Matlab

I am trying to figure out how to run the camera and stop the camera at the push of a button. If I cant do that, how do I set up camera to run within this figure and I will have it run all the time. Also I need to capture an image.
function faceCam2()
vid = videoinput('winvideo');
% Create a figure window
hFig = figure('Toolbar','none',...
'Menubar', 'none',...
'NumberTitle','Off',...
'Name','FaceScan');
%start camera
uicontrol( 'String', 'Start Preview',...
'Callback', 'preview(vid)',...
'Units','normalized',...
'Position',[0 0 0.15 .07]);
%stop
uicontrol( 'String', 'Stop Preview',...
'Callback', 'stoppreview(vid)',...
'Units','normalized',...
'Position',[.17 0 .15 .07]);
%snapshot
uicontrol( 'String', 'Pic',...
'Callback', 'data = getsnapshot(vid)',...
'Units','normalized',...
'Position',[0.34 0 .15 .07]);
%close window
uicontrol( 'String', 'Close',...
'Callback', 'close(gcf)',...
'Units','normalized',...
'Position',[0.51 0 .15 .07]);
end
I get the Error in matlab command window when I press a button. Quit works, but not the other 3. What do I need to do?
Thanks!
Undefined function or variable 'vid'.
Error while evaluating uicontrol Callback
The problem is that vid is a local variable in the function faceCam2 and is not visible to the callback. Here are a few methods to pass data to callback functions. When using nested methods, the code looks like this:
function faceCam2()
vid = videoinput('winvideo');
% Create a figure window
hFig = figure('Toolbar','none',...
'Menubar', 'none',...
'NumberTitle','Off',...
'Name','FaceScan');
%start camera
uicontrol( 'String', 'Start Preview',...
'Callback', #prevCallback,...
'Units','normalized',...
'Position',[0 0 0.15 .07]);
function prevCallback(hObject,eventdata)
preview(vid);
end
%...
end