No BSD License  

Small calculater GUI

by

 

A small, simple calculator designed to show some simple techniques.

gui1
%CREATE GUI GRAPHICS
function gui1
gui_singleton=1;
gui_state=struct('gui_Name', mfilename,...
        'gui_Singleton', gui_singleton, ...
        'gui_OpeningFcn', @gui_openingfcn, ...
        'gui_OutputFcn', @gui_outputfcn, ...
        'gui_LayoutFcn', [] , ...
        'gui_Callback', []);

fig=figure('name','My GUI','visible','off','position',[360 500 160 150],'menubar','none','tag','fig');

b1=uicontrol(fig,...
    'style','pushbutton',...
    'string','Calculate',...
    'position',[20 40 50 15],...
    'tag','b1',...
    'callback',{@calcbutton});
m1=uicontrol(fig,...
    'style','pop',...
    'enable','on',...
    'string',{1,2,3,4,5,6,7,8,9},...
    'position',[20 120 30 5],...
    'tag','m1',...
    'value',1);
m2=uicontrol(fig,...
    'style','pop',...
    'enable','on',...
    'string',{1,2,3,4,5,6,7,8,9},...
    'position',[110 120 30 5],...,...
    'tag','m2');
t2=uicontrol(fig,...
    'style','text',...
    'string','Result:',...
    'position',[20 70 40 15],...,...
    'tag','t2',...
    'BackgroundColor','white');
t3=uicontrol(fig,...
    'style','text',...
    'position',[65 70 40 15],...,...
    'tag','t3',...
    'BackgroundColor','white');
b2=uicontrol(fig,...
    'style','pushbutton',...
    'string','Reset',...
    'position',[80 40 40 15],...,...
    'tag','b2',...
    'callback',{@resetbutton});
m3=uicontrol(fig,...
    'style','pop',...
    'string',{'add','minus','times','divide'},...,...
    'tag','m3',...
    'position',[55 120 50 5]);
b3=uicontrol(fig,...
    'style','pushbutton',...
    'string','Save',...
    'position',[20 20 40 15],...,...
    'tag','b3',...
    'callback',{@savebutton});
b4=uicontrol(fig,...
    'style','pushbutton',...
    'string','Load',...
    'position',[80 20 40 15],...,...
    'tag','b4',...
    'callback',{@loadbutton});

%INITIALISE
value1=1;
value2=1;
str3='add';
val3=1;
% movegui(fig,'center')
set([fig,b1,m1,m2,t2,t3,b2],'units','normalized','visible','on');

%CALCULATIONS
%     function pop1cb(source,eventdata); %call popup value 1
%         str1=get(source,'string');
%         val1=get(source,'value');
%         data1=str1(val1);
%         value1=str2double(data1);
%     end
%     function pop2cb(source,eventdata); %call popup value 2
%         str2=get(source,'string');
%         val2=get(source,'value');
%         data2=str2(val2);
%         value2=str2double(data2);
%     end
    function calcbutton(source,eventdata); %push to calculate button
        str3=get(m3,'string');
        val3=get(m3,'value');
        
        str1=get(m1,'string');
        val1=get(m1,'value');
        data1=str1(val1);
        value1=str2double(data1);
        
        str2=get(m2,'string');
        val2=get(m2,'value');
        data2=str2(val2);
        value2=str2double(data2);
        
        switch str3{val3};
            case 'add'
                tot=value1+value2;
            case 'minus'
                tot=value1-value2;
            case 'times'
                tot=value1*value2;
            case 'divide'
                tot=value1/value2;
        end
        set(t3,'string',tot);
%         data=guihandles(fig)
%         guidata(fig, data)
    end
    function resetbutton(source,eventdata);
        val=1;
        blank='';
        value1=1;
        value2=1;
        set(t3,'string',blank);
        set(m1,'value',val);
        set(m2,'value',val);       
        set(m3,'value',val);
    end
    function savebutton(source,eventdata);
        savestate(fig);
    end
    function loadbutton(source,eventdata);
        restorestate(fig);
    end
    function savestate(fig);
        state.m1=get(m1, 'value');
        state.m2=get(m2, 'value');
        state.m3=get(m3, 'value');
        
        save fig.mat state
    end
    function restorestate(fig);
        load fig.mat state;
        
        set(m1, 'value', state.m1);
        set(m2, 'value', state.m2);
        set(m3, 'value', state.m3);
        calcbutton(fig);
    end
end

Contact us