Hello, I'm extremely new to Matlab and I was looking for some help on a calculator I'm creating.

I have very limited knowledge on how to program in matlab and any help would be appreciated. I am trying to learn how to make Gui's without the use of GUIDE. so far I've created a figure with all of the different buttons I plan on using so far, as well as a static text screen at the top of the gui. I was wondering how to make it so when I press, for example the 1 button, it will display the string 1 at the top of my calculator.
Here is my code so far.
------------------------------------------------------------------------
function calcgui (action, h0)
%calls newdb
db = newdb();
gui = db.gui;
%Creates the calcgui parent figure.
Figh = figure('name', 'Calcgui',...
'visible', 'on',...
'pointer', 'cross',...
'numbertitle', 'off',...
'MenuBar', 'none',...
'Position',[1290 50 300 400],...
'color', db.color.bground,...
'units','points');
%Children for the figure representing buttons 0 through 9
%========================================================
uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [4, 4, 50, 35],...
'tag', 'decimal',...
'String', '.',...
'fontweight', 'bold',...
'fontsize', 10,...
'Callback', {@callback_ParentAction});
% str = get(handles.output,'string');
% str = strcat(str, '0');
% set(handles.output, 'string',str);
uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [54, 4, 50, 35],...
'tag', 'zero',...
'String', '0',...
'fontweight', 'bold',...
'fontsize', 10,...
'Callback', {@callback_ParentAction});
uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [104, 4, 50, 35],...
'tag', 'equals',...
'String', '=',...
'fontweight', 'bold',...
'fontsize', 10,...
'Callback', {@callback_ParentAction});
uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [4, 39, 50, 35],...
'tag', 'one',...
'String', '1',...
'fontweight', 'bold',...
'fontsize', 10,...
'Callback', {@callback_ParentAction});
uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [54, 39, 50, 35],...
'tag', 'two',...
'String', '2',...
'fontweight', 'bold',...
'fontsize', 10,...
'Callback', {@callback_ParentAction});
uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [104, 39, 50, 35],...
'fontweight', 'bold',...
'tag', 'three',...
'String', '3',...
'fontsize', 10,...
'Callback', {@callback_ParentAction});
uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [4, 74, 50, 35],...
'tag', 'four',...
'String', '4',...
'fontweight', 'bold',...
'fontsize', 10,...
'Callback', {@callback_ParentAction});
uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [54, 74, 50, 35],...
'tag', 'five',...
'String', '5',...
'fontweight', 'bold',...
'fontsize', 10,...
'Callback', {@callback_ParentAction});
uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [104, 74, 50, 35],...
'tag', 'six',...
'String', '6',...
'fontweight', 'bold',...
'fontsize', 10,...
'Callback', {@callback_ParentAction});
uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [4, 109, 50, 35],...
'tag', 'seven',...
'String', '7',...
'fontweight', 'bold',...
'fontsize', 10,...
'Callback', {@callback_ParentAction});
uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [54, 109, 50, 35],...
'tag', 'eight',...
'String', '8',...
'fontweight', 'bold',...
'fontsize', 10,...
'Callback', {@callback_ParentAction});
uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [104, 109, 50, 35],...
'fontsize', 10,...
'fontweight', 'bold',...
'tag', 'nine',...
'String', '9',...
'Callback', {@callback_ParentAction});
%=======================================================
% plus multiply subtract and divide
uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [154, 4, 67, 35],...
'tag', 'subtract',...
'String', '-',...
'fontsize', 18,...
'Callback', {@callback_ParentAction});
uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [154,39, 67, 35],...
'fontsize', 12,...
'tag', 'plus',...
'String', '+',...
'Callback', {@callback_ParentAction});
uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [154,74, 67, 35],...
'fontsize', 12,...
'tag', 'times',...
'String', 'x',...
'Callback', {@callback_ParentAction});
handles.buttontimes = uicontrol(Figh,...
'Style', 'pushbutton', ...
'Units', 'points',...
'Position', [154,109, 67, 35],...
'fontsize', 10,...
'tag', 'division',...
'String', '/',...
'Callback', {@callback_ParentAction});
%==========================================================
%output screen
handles.output = uicontrol(Figh,...
'Style', 'text', ...
'Units', 'points',...
'Position', [4,240, 217, 55],...
'fontsize', 35,...
'tag', 'output',...
'String', '',...
'HorizontalAlignment', 'right',...
'Callback', {@callback_ParentAction});
set(handles.output)
function rounded = round_pts(points)
%FLOOR_PTS(structure) Floors pixel count instead of default
% rounding of points. Performs operation on
% all integer lengths stored in a struct.
%
%floor_pts(structure)
persistent pt_per_px;
if isempty(pt_per_px)
pt_per_px = 72 / get(0,'ScreenPixelsPerInch');
end
remainder = rem(points, pt_per_px);
rounded = points - remainder;
if remainder > 0.5 * pt_per_px
rounded = rounded + pt_per_px;
end
function db = newdb()
%NEWDB Add calcgui info to an existing db struct.
%
%newdb();
db = struct();
% Colors
db.color.gray = [1 1 1] * 0.9;
db.color.darkgray = db.color.gray * .75;
db.color.vbcolor = [1 1 1] * 0.75; % Uncomment for GUI development purposes
db.color.bgcolor = [1 1 1] * 0.4;
db.color.frcolor = [.1 .2 .3];
db.color.bground = [0 .2 .5];
db.color.frcolor = 0.7 * db.color.bgcolor;
% Define DIMGUI Figure window dimensions.
buffer_space = round_pts(10); % space between frame/figure edges
gui.fig0r = round_pts(100); % main window x offset from left of window
gui.fig0u = round_pts(100); % main window y offset from bottom of window
gui.fig0w = round_pts(525); % main window width
gui.fig0h = round_pts(525); % main window height
db.gui = gui;
------------------------------------------------------------------
Thanks, Tim

Answers (1)

Tim - if you are not going to use GUIDE to develop your GUI, you can try to do the following. Move your round_pts and newdb functions outside of your main calcgui function (so add an end statement to each of your three functions) or move them into their own m files.
Now nest your callback_ParentAction function within calcgui so that it has access to all of the objects/controls that are created by the GUI (and in particular the handle to the text box that acts as the output for the button presses). The callback (for each button) could look something like
function callback_ParentAction(hObject, ~)
digit = get(hObject,'String');
if ~isnan(str2double(char(digit)))
currentOutput = char(get(handles.output,'String'));
set(handles.output,'String',[currentOutput digit]);
end
end
The above function will fire on the press of any button within your calculator. If the button is numeric (given its digit value which, when converted from a string to a number, will not be NaN) then we can append this value to whatever has already been added to the output "window". (You would need to update this function to handle the other key presses.) Try this and see what happens!
(Note that the str2double(char(digit)) will be NaN if the string (i.e. the text of the button) is not numeric.)

Categories

Asked:

on 17 Jun 2015

Answered:

on 17 Jun 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!