Make M-file attached to a guide

2 views (last 30 days)
IPSAS
IPSAS on 11 May 2012
I work with MATLAB 2007A and I want to put this M-file attached to a guide? helpme please this is a job for my final year of study
function draw_lines
% Click the left mouse button to define a point
% Drag the mouse to draw a line to the next point and
% left click again
% Right click the mouse to stop drawing
ComId = Com_construct;
OmniDriveId= OmniDrive_construct;
OdometryId = Odometry_construct;
BumperId = Bumper_construct;
fh = figure('units','pixels',...
'position',[100 100 800 500],...
'menubar','none',...
'name','figure',...
'numbertitle','off',...
'resize','on',...
'WindowButtonDownFcn',@wbdcb);
dist= uicontrol(fh,'Style','edit',...
'String','',...
'backgroundcolor','r',...
'Position',[200 450 130 20]);
posX0= uicontrol(fh,'Style','edit',...
'String','',...
'backgroundcolor','r',...
'Position',[20 450 130 20]);
posY0= uicontrol(fh,'Style','edit',...
'String','',...
'backgroundcolor','r',...
'Position',[20 420 130 20]);
posX1= uicontrol(fh,'Style','edit',...
'String','',...
'backgroundcolor','b',...
'Position',[20 390 130 20]);
posY1= uicontrol(fh,'Style','edit',...
'String','',...
'backgroundcolor','b',...
'Position',[20 360 130 20]);
buttonconnecter = uicontrol(fh,'style','push',...
'units','pix',...
'position',[10 100 180 40],...
'fontsize',14,...
'string','connecter',...
'callback',@connect);
buttonmarcher = uicontrol(fh,'style','push',...
'units','pix',...
'position',[10 50 180 40],...
'fontsize',14,...
'string','marcher',...
'callback',@marche);
buttonarreter = uicontrol(fh,'style','push',...
'units','pix',...
'position',[10 10 180 40],...
'fontsize',14,...
'string','arret',...
'callback',@arret);
buttonclear = uicontrol(fh,'style','push',...
'units','pix',...
'position',[10 150 180 40],...
'fontsize',14,...
'string','clear',...
'callback',@clear);
ah = axes('Parent',fh,'Position',[.25 .05 .7 .7],...
'DrawMode','fast');
axis ([1 1000 1 1000])
function wbdcb(src,evnt)
if strcmp(get(src,'SelectionType'),'normal')
[x,y,str] = disp_point(ah);
hl = line('XData',x,'YData',y,'Marker','.');
text(x,y,str,'VerticalAlignment','bottom');drawnow
set(src,'WindowButtonMotionFcn',@wbmcb)
elseif strcmp(get(src,'SelectionType'),'alt')
set(src,'WindowButtonMotionFcn','')
[x,y,str] = disp_point(ah);
text(x,y,str,'VerticalAlignment','bottom');drawnow
end
function wbmcb(src,evnt)
[xn,yn,str] = disp_point(ah);
xdat = [x,xn];
ydat = [y,yn];
set(hl,'XData',xdat,'YData',ydat);
set(posX0,'string',num2str(xdat(1:1),'%0.3g'));
set(posY0,'string',num2str(ydat(1:1),'%0.3g'));
end
end
function [x,y,str] = disp_point(ah)
cp = get(ah,'CurrentPoint');
x = cp(1,1);y = cp(1,2);
str = ['(',num2str(x,'%0.3g'),', ',num2str(y,'%0.3g'),')'];
set(posX1,'string',num2str(x,'%0.3g'));
set(posY1,'string',num2str(y,'%0.3g'));
end
function connect (h,evnt)
Com_setAddress(ComId, '127.0.0.1:8080'); % 172.26.1.1 127.0.0.1:8080
Com_connect(ComId);
Odometry_set(OdometryId, 0, 0, 0);
end
function marche(h,evnt)
x0=str2double(get(posX0,'string'));
y0=str2double(get(posY0,'string'));
OmniDrive_setComId(OmniDriveId, ComId);
Odometry_setComId( OdometryId, ComId );
Bumper_setComId(BumperId, ComId);
tStart = tic;
Odometry_set(OdometryId, x0, y0, 0);
a=str2double(get(posX1,'string'));
b=str2double(get(posY1,'string'));
while (Bumper_value(BumperId) ~= 1)
tElapsed = toc(tStart);
% % % If 60 seconds are elapsed then exit while loop
if(tElapsed >= 60 )
break;
end;
%Odometry_set(OdometryId, x0, y0, 0);
[x, y, phi]= Odometry_get(OdometryId);
%xlswrite('testdata.xls', x, 1, 'A1')
Dy= b - y0;
Dx= a - x0;
D= sqrt(Dx.^2+Dy.^2);
set(dist,'string',num2str(D,'%0.3g'));
if ((Dx==0) && (Dy<0))
while (phi<=270)
[x, y, phi]= Odometry_get(OdometryId);
OmniDrive_setVelocity(OmniDriveId, 0, 0 ,-50);
end;
elseif ((Dx==0) && (Dy>0))
while (phi<=90)
[x, y, phi]= Odometry_get(OdometryId);
OmniDrive_setVelocity(OmniDriveId, 0, 0 ,50);
end;
else
angle= atan(Dy/Dx);
phi1= rad2deg(angle);
if (phi1<0)
while (phi>phi1)
[x, y, phi]= Odometry_get(OdometryId);
OmniDrive_setVelocity(OmniDriveId, 0, 0 ,-50);
end;
elseif (phi1>1)
while (phi<=phi1)
[x, y, phi]= Odometry_get(OdometryId);
OmniDrive_setVelocity(OmniDriveId, 0, 0 ,50);
end;
else
OmniDrive_setVelocity(OmniDriveId, 0, 0 ,0);
end;
end;
[x, y, phi]= Odometry_get(OdometryId);
Dyy= b - y;
Dxx= a - x;
%while (D>0)
if ((Dxx<=0)&& (Dyy<=0))
OmniDrive_setVelocity(OmniDriveId, 0, 0 ,0);
break;
else
OmniDrive_setVelocity(OmniDriveId, 800, 0 ,0);
[x, y, phi]= Odometry_get(OdometryId);
line(x,y,'Marker','.');
% xlswrite('testdata.xls', x, 1, 'A1')
end;
%end;
end;
end
function clear(src,evt)
cla(ah,'reset')
%lbs = '';
set(ah,'DrawMode','fast');
axis ([1 1000 1 1000])
end
function arret(h,evnt)
Com_disconnect(ComId);
close(fh);
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 11 May 2012
In GUIDE, create a single button named "GO". In the callback code that is generated for that button, call your routine. Your routine will create the additional figures and controls that it needs, when the routine executes.
  2 Comments
Image Analyst
Image Analyst on 11 May 2012
Yes, but I'd recommend getting rid of all those uicontrols() in that code and just plop the controls down in GUIDE via the toolbar icons. It's much easier than constructing all those things via code, particularly if you want to resize them or move them around later.
Walter Roberson
Walter Roberson on 11 May 2012
I would say exactly the opposite: drop GUIDE completely for this project, as it is much easier to move objects around and resize them via code than to fuss with GUIDE.
I run GUI layout programmatically, autosizing controls (including text uicontrols) according to their content (and automatically taking into account font size.) My programs place controls relative to each's actual size. Alignment all taken care of by function. This gives, for example, meaningful down-sizing of the window without having to shrink text font: if a field needs another line to be readable, the routines give it another line.
Note in this regard that screen areas sometimes get repurposed during execution of my GUIs. Option A needs inputs that Option B doesn't need, and Option C needs different inputs yet. This kind of situation is not difficult to set up programmatically, but GUIDE is not adept at handling items that overlap spatially.
There might be an excuse if GUIDE supported (allowed the user to create) one of the alternative paradigms such as scrollable panes, but it doesn't. The methodology of placing everything at a constant location relative to the figure is a problem when one considers items that need to maintain pretty much constant size (e.g., text, video preview) interacting with relatively-placed items.

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!