Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

plot in gui

Status
Not open for further replies.

macgywer

Industrial
Jun 23, 2006
1
is there any way i could make this program plot my own functions? e.g. x^2 + 3x ... i think something like changing the listbox in function chooser to en edit box? any suggestions?





function GUIdemo(fcn)

%demo of user interface construction
%this function CALLS ITSELF via GUI callbacks

% By default make the GUI
%This code detects the first entry into the function
%from the command line with no parameters
if nargin == 0
fcn = 'makeGUI';
end

%This is the main decision point of the function.
%The switch statement is executed once-per-fuction call
switch fcn

%This code is executed ONCE when the function enters with
%no arguments
case 'makeGUI'

% Determine the name of this function and store it in the
%figure plotinfo variable.
%Since variables used in a function are not persistent after
%the function exits we will need to store the state-variables
%in a data structure associated with the persistent Figure-window.
%The plotinfo sturcture will be saved into the Figure's UserData
%area and retrieved from there as necessary.
plotinfo.myname = mfilename;

% ===Create main figure==========================
fig = figure('Position',centerfig(660,630),...
'Resize','off',...
'NumberTitle','off',...
'Name','GUI demo',...
'Interruptible','off',...
'Menubar','none',...
'Color',get(0,'DefaultUIControlBackgroundColor'));

%===Header text===================================
uicontrol(gcf,'Style','text', ...
'String','A GUI example',...
'fontsize',20, ...
'HorizontalAlignment','Center',...
'Position',[200,600,220,30],...
'BackgroundColor',[0.8 1 0.8]);

%===A menu======================================
cmenu=uimenu(gcf,'label','figure color');
uimenu(cmenu,...
'label','grey',...
'callback','set(gcf,''color'',''default'')');
uimenu(cmenu,...
'label','yellow',...
'callback','set(gcf,''color'',''yellow'')');
uimenu(cmenu,...
'label','black',...
'callback','set(gcf,''color'',''black'')');

% ===Create axes=================================
plotinfo.ax = axes('Units','pixels',...
'Position',[235 165 395 360],...
'Box','on',...
'XLim',[0 1],'YLim',[-1 1]);
xlabel('x'); ylabel('y');

% ===The quit button===============================
uicontrol('Style','pushbutton',...
'Position',[595 595 45 25],...
'String','Quit',...
'Interruptible','off',...
'BusyAction','cancel',...
'Callback',[plotinfo.myname,' quit']);

%==Frequency slider===========================
plotinfo.freq=1.0;
plotinfo.s1 = uicontrol(gcf,'Style','text', ...
'String','frequency',...
'Position',[10,240,100,20],...
'BackgroundColor',[0.8,0.8,0.8]);
plotinfo.s2 = uicontrol(gcf,'Style','edit',...
'String',num2str(plotinfo.freq),...
'Position',[110,240,50,20],...
'BackgroundColor',[0.8,0.8,0.8],...
'callback', [plotinfo.myname,' editfreq']);
plotinfo.s3 = uicontrol(gcf,...
'Style','slider',...
'Min' ,1,'Max',20, ...
'Position',[10,220,150,20], ...
'Value', 1,...
'SliderStep',[0.01 0.1], ...
'BackgroundColor',[0.8,0.8,0.8],...
'CallBack', [plotinfo.myname,' setfreq']);

%==Draw button===========================
uicontrol(gcf,'Style','Pushbutton', ...
'String','Draw',...
'Position',[10,450,70,20],...
'BackgroundColor',[1,0.8,0.8], ...
'CallBack',[plotinfo.myname,' draw'] );

%==Function Chooser=========================
plotinfo.fun='sin';
plotinfo.choose=uicontrol(gcf,'Style','listbox', ...
'String','sin|cos|tan',...
'Position',[10,390,70,50],...
'BackgroundColor',[0.8,0.8,0.8], ...
'CallBack',[plotinfo.myname,' choose'] );

%==Open a dialog box========================
uicontrol('style','text',...
'string','Dialog box chooser',...
'backgroundcolor',[.8,.8,.9],...
'position',[10,40,70,50]);
plotinfo.dlg='1';
plotinfo.number{1}=' no number yet!';
plotinfo.dlgbox=uicontrol(gcf,'Style','popup', ...
'String','error|help|input|msgbox|question',...
'Position',[10,10,70,50],...
'BackgroundColor',[0.8,0.8,1], ...
'CallBack',[plotinfo.myname,' dialog'] );

%==radiobutton===================================
plotinfo.red=0;
plotinfo.rbut=uicontrol(gcf,'Style','checkbox', ...
'String','Red axes',...
'Position',[10,300,70,50],...
'BackgroundColor',[1,1,.8], ...
'CallBack',[plotinfo.myname,' redaxes'] );

%==axes title====================================
plotinfo.title='Use edit field to change this title';
plotinfo.ttl=uicontrol(gcf,'Style','edit', ...
'String','Edit Figure title',...
'Position',[10,130,150,20],...
'BackgroundColor',[1,1,1], ...
'CallBack',[plotinfo.myname,' edttl'] );

%==Context sensitive menu==========================
%====Also note reference to this menu in the plot==
% Define the context menu (taken from Matlab docs)
plotinfo.cmenu = uicontextmenu;
% Define the context menu items
plotinfo.item1 = uimenu(plotinfo.cmenu, 'Label', 'dashed', ...
'Callback',[plotinfo.myname,' linemenu1']) ;
plotinfo.item2 = uimenu(plotinfo.cmenu, 'Label', 'dotted', ...
'Callback', [plotinfo.myname,' linemenu2']);
plotinfo.item3 = uimenu(plotinfo.cmenu, 'Label', 'solid',...
'Callback', [plotinfo.myname,' linemenu3']);
uicontrol('style','text',...
'string','Hit Draw button, then right-click the plot line for options',...
'backgroundcolor','white',...
'position',[350,100,150,40]);


%put all the variables in a safe place (the figure's data area)
set(fig,'UserData',plotinfo);

case 'draw'
x=0:0.001:1;
amp=1.0;
%Get data from the figure's data area
plotinfo=get(gcf,'UserData');
y=eval(strcat(plotinfo.fun,'(2*pi*plotinfo.freq*x)*amp'));
plotinfo.line=plot(x,y,'black',....
'uicontextmenu',plotinfo.cmenu);
set(plotinfo.rbut,'value',0);
title(plotinfo.title);
set(gcf,'UserData',plotinfo);

case 'quit'
fig = gcf;
quit_reply = questdlg('Really quit this lame example?');
if strcmp(quit_reply,'Yes')
close(fig);
end

case 'setfreq'
%Get data from the figure's data area
plotinfo=get(gcf,'UserData');
%Get the value from the slider
plotinfo.freq=get(plotinfo.s3,'Value');
%Update the text which shows the slider value
set(plotinfo.s2,'String',plotinfo.freq);
%Store the new slider value back into the figure's data area
set(gcf,'UserData',plotinfo);

case 'editfreq'
plotinfo=get(gcf,'UserData');
plotinfo.freq=str2num(get(plotinfo.s2,'string'));
set(plotinfo.s3,'value',plotinfo.freq);
set(gcf,'UserData',plotinfo);

case 'choose'
plotinfo=get(gcf,'UserData');
plotinfo.fun=get(plotinfo.choose,'value');
switch plotinfo.fun
case 1
plotinfo.fun='sin';
case 2
plotinfo.fun='cos';
case 3
plotinfo.fun='tan';
end
set(gcf,'UserData',plotinfo);

case 'dialog'
plotinfo=get(gcf,'UserData');
plotinfo.dlg=get(plotinfo.dlgbox,'value');
switch plotinfo.dlg
case 1
errordlg('Ouch!, now you''ve done it!');
case 2
helpdlg('Feel the Force, Luke.');
case 3
plotinfo.number=inputdlg('Enter a number');
case 4
msgbox(strcat...
('The last number you input was ',...
plotinfo.number{1}));
case 5
questdlg('Have you called your mom lately?');

end
set(gcf,'UserData',plotinfo);

case 'redaxes'
plotinfo=get(gcf,'UserData');
plotinfo.red=get(plotinfo.rbut,'Value');
if plotinfo.red
set(gca,'color','red');
else
set(gca,'color','white');
end
set(gcf,'UserData',plotinfo);

case 'edttl'
plotinfo=get(gcf,'UserData');
plotinfo.title=get(plotinfo.ttl,'string');
title(plotinfo.title);
set(gcf,'UserData',plotinfo);

case 'linemenu1'
plotinfo=get(gcf,'UserData');
set(plotinfo.line, 'LineStyle', '--')

case 'linemenu2'
plotinfo=get(gcf,'UserData');
set(plotinfo.line, 'LineStyle', ':')

case 'linemenu3'
plotinfo=get(gcf,'UserData');
set(plotinfo.line, 'LineStyle', '-')

end

%===A utility to center the window on the screen============
function pos = centerfig(width,height)

% Find the screen size in pixels
screen_s = get(0,'ScreenSize');
pos = [screen_s(3)/2 - width/2, screen_s(4)/2 - height/2, width, height];
 
Replies continue below

Recommended for you

The basic idea is to transfer the function as a string, and then matlab will process it as a command line.
What you might need to add ar boxes which will define plot variable, limits and other parameters, and then put then all into a matlab compatible string, and let matlab do the hard work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor