|
« Earlier | 9 items total |
Later » |
Here is an example from the newsgroupbut could be probably done neater with nested functions.
function test
% test gui function
% create figure
fig = figure;
% set the figure's WindowButtonDownFcn
set(fig,'WindowButtonDownFcn',{@wbd});
end
function wbd(h,evd)
disp('down')
% get the values and store them in the figure's appdata
props.WindowButtonMotionFcn = get(h,'WindowButtonMotionFcn');
props.WindowButtonUpFcn = get(h,'WindowButtonUpFcn');
setappdata(h,'TestGuiCallbacks',props);
% set the new values for the WindowButtonMotionFcn and
% WindowButtonUpFcn
set(h,'WindowButtonMotionFcn',{@wbm})
set(h,'WindowButtonUpFcn',{@wbu})
end
function wbm(h,evd)
% executes while the mouse moves
disp('motion')
end
function wbu(h,evd)
% executes when the mouse button is released
disp('up')
% get the properties and restore them
props = getappdata(h,'TestGuiCallbacks');
set(h,props);
end
If you want to show how your plots change with time, a movie is a good idea. Especially since MATLAB can produce movies that can be embedded in your presentations, or be seen with any good ol' movie player. Here's how: Courtesy of http://www.ecogito.net/matlab/The idea is to create plot figures and compile them as frames of your movie.
% Create a new figure, and position it
fig1 = figure;
winsize = get(fig1,'Position');
winsize(1:2) = [0 0];
% Create movie file with required parameters
fps= 25;
outfile = sprintf('%s',outFileName)
mov = avifile(outfile,'fps',fps,'quality',100);
% Ensure each frame is of the same size
set(fig1,'NextPlot','replacechildren');
for i=1:numframes
plot(x,y); % generate your plot
% put this plot in a movieframe
% In case plot title and axes area are needed
% F = getframe(fig1,winsize);
% For clean plot without title and axes
F = getframe;
mov = addframe(mov,F);
end
% save movie
mov = close(mov);
Voila! (Make sure that you do not open any window over your plot window while MATLAB is compiling your movie, else those windows will become part of your movie)
Use ginput function:
x=0;y=0;
while ~isempty(x)
[x1,y1]=ginput(1);
plot([x x1],[y y1],'b.-');
hold on
x=x1;y=y1;
end
Use char function:
txt=char('First line','Second line','etc');
title(txt);
The char function is used here to consolidate strings of different length into a single array. You might wish to play around with the alignment:
h=title(txt);
set(h,'HorizontalAlignment','Right');
set(h,'VerticalAlignment','Middle');
Simple function to draw a voxel (cube, cuboid) in a specific position of specific dimensions in a 3-D plot. Transparency of the voxel can also be specified. Many voxels can be aggregated to form a different shapes (a simple 3-D "+" is shown in the screenshot). Written by Joel Suresh
function voxel(i,d,c,alpha);
%VOXEL function to draw a 3-D voxel in a 3-D plot
%
%Usage
% voxel(start,size,color,alpha);
%
% will draw a voxel at 'start' of size 'size' of color 'color' and
% transparency alpha (1 for opaque, 0 for transparent)
% Default size is 1
% Default color is blue
% Default alpha value is 1
%
% start is a three element vector [x,y,z]
% size the a three element vector [dx,dy,dz]
% color is a character string to specify color
% (type 'help plot' to see list of valid colors)
%
%
% voxel([2 3 4],[1 2 3],'r',0.7);
% axis([0 10 0 10 0 10]);
%
% Suresh Joel Apr 15,2003
% Updated Feb 25, 2004
switch(nargin),
case 0
disp('Too few arguements for voxel');
return;
case 1
l=1; %default length of side of voxel is 1
c='b'; %default color of voxel is blue
case 2,
c='b';
case 3,
alpha=1;
case 4,
%do nothing
otherwise
disp('Too many arguements for voxel');
end;
x=[i(1)+[0 0 0 0 d(1) d(1) d(1) d(1)]; ...
i(2)+[0 0 d(2) d(2) 0 0 d(2) d(2)]; ...
i(3)+[0 d(3) 0 d(3) 0 d(3) 0 d(3)]]';
for n=1:3
if n==3,
x=sortrows(x,[n,1]);
else
x=sortrows(x,[n n+1]);
end;
temp=x(3,:);
x(3,:)=x(4,:);
x(4,:)=temp;
h=patch(x(1:4,1),x(1:4,2),x(1:4,3),c);
set(h,'FaceAlpha',alpha);
temp=x(7,:);
x(7,:)=x(8,:);
x(8,:)=temp;
h=patch(x(5:8,1),x(5:8,2),x(5:8,3),c);
set(h,'FaceAlpha',alpha);
end
I track visitors to my site using google analytics and I noticed I had 8 visitors to my site last week looking for how to draw a circle. However I didn't have any circle drawing snippets. So I have borrowed some code from the Mathworks file exchange. This code is courtesy of Zhenhai Wang.
function H=circle(center,radius,NOP,style)
%-----------------------------------------------------
% H=CIRCLE(CENTER,RADIUS,NOP,STYLE)
% This routine draws a circle with center defined as
% a vector CENTER, radius as a scaler RADIS. NOP is
% the number of points on the circle. As to STYLE,
% use it the same way as you use the rountine PLOT.
% Since the handle of the object is returned, you
% use routine SET to get the best result.
%
% Usage Examples,
%
% circle([1,3],3,1000,':');
% circle([2,4],2,1000,'--');
%
% Zhenhai Wang <zhenhai@ieee.org>
% Version 1.00
% December, 2002
%-----------------------------------------------------
if (nargin <3),
error('Please see help for INPUT DATA.');
elseif (nargin==3)
style='b-';
end;
THETA=linspace(0,2*pi,NOP);
RHO=ones(1,NOP)*radius;
[X,Y] = pol2cart(THETA,RHO);
X=X+center(1);
Y=Y+center(2);
H=plot(X,Y,style);
axis square;
Using the layout tool from XTargets you can write code like this
f = figure('units','pixels','position',[10 10 500 500]);
layout = xtargets_borderlayout(f);
layout.add(uicontrol('units','pixels','string','north'),'north');
layout.add(uicontrol('units','pixels','string','south'),'south');
layout.add(uicontrol('units','pixels','string','east'),'east');
layout.add(uicontrol('units','pixels','string','west'),'west');
layout.add(uicontrol('units','pixels','string','centre'),'centre');
And it creates a panel like in this linkSee the full tutorial for more information
Here is a simple way to use java tables in matlab. I've wrapped the standard java table model with a matlab data model. Nested functions are taken advantage of to provide data set and get methods, cell rendering and entry validation.
% XTARGETS_UITABLE
%
% t = xtargets_uitable(table, rows, cols, getfun, setfun, labelfun, editfun)
%
% Arguments
% table Any instance or subclass instance of javax.swing.JTable
% rows numbers of rows
% cols number of cols
% getfun function handle for aquiring data to the table.
% val = getfun(row, col)
% setfun function handle for setting data to the table.
% setfun(row, col, val)
% labelfun function handle for generating a label in the table
% string = labelfun(row, col, val)
% editfun function handle for validating string input
% value = editfun(row, col, string)
% throw an error if the string is invalid
% to show a dialog box and reset the old value
% of the cell
% table an instance of javax.swing.JTable or a subclass of
%
% Example
% See xtargets_uitable_test
%
% Copyright Brad Phelan (C) 2005 under the LGPL license
% http://xtargets.com
function t = xtargets_uitable(table, rows, cols, getfun, setfun, labelfun, editfun)
import javax.swing.*;
import javax.swing.table.*;
import ca.odell.renderpack.*;
tableModel = javax.swing.table.DefaultTableModel(rows,cols);
% Initialize the table model
for r = 1:rows
for c = 1:cols
tableModel.setValueAt(labelfun(r,c,getfun(r,c)),r-1,c-1);
end
end
table.setModel(tableModel);
scroller = JScrollPane(table);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
[hcom, hg] = javacomponent(scroller);
hg = handle(hg);
p = schema.prop(hg,'table','mxArray');
hg.table = handle(table);
set(hg,'position',[0 0 200 200]);
%Fix for JTable focus bug : see http://www.mycgiserver.com/~Kleopatra/swing/table/merlineditfun.html
table.putClientProperty('terminateEditOnFocusLost', java.lang.Boolean.TRUE);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
t = hg;
% A flag to avoid change handler recursion
syncchange = false;
set(tableModel, 'TableChangedCallback', @change_fcn);
function change_fcn(src, ev)
if syncchange
% This change was invoked from this handler
syncchange = false;
return
end
syncchange = true;
row = ev.getFirstRow;
col = ev.getColumn;
string = tableModel.getValueAt(row,col);
row = row + 1;
col = col + 1;
% Perform validation on the string and convert it to a value
try
val = editfun(row, col, string);
setfun(row,col,val);
tableModel.setValueAt(labelfun(row,col,val), row-1,col-1);
catch
errordlg(lasterr,'Invalid Edit');
% The value was not valid so reset the cell
tableModel.setValueAt(labelfun(row,col,getfun(row,col)), row-1,col-1);
end
end
end
A test function demonstrates the behaviour of the table with a simple data model and validation.
% xtargets_uitable_test
%
% A simple example of using the table model to modify data in
% a nested function workspace. The data is in double format
% so we use the '%g' sscanf pattern to render and read the
% data from the table cells.
% Copyright Brad Phelan (C) 2005 under the LGPL license
% http://xtargets.com
function xtargets_uitable_test
close all;
data = rand(5);
table = javax.swing.JTable;
% Create a uitable with a simple matlab data model. Specify
% that the conversion class will be %g.
t = xtargets_uitable(table, 5,5, @getfun, @setfun, @labelfun, @editfun);
function val = getfun( row, col)
val = data(row,col);
end
function setfun( row, col, val)
data(row,col) = val;
end
function val = editfun(row, col, string)
val = sscanf(string, '%g');
if isempty(val)
error([ '''' string ''' is not a valid entry' ]);
end
end
function label = labelfun(row, col, val)
label = sprintf('%g',val);
end
% uicontrol to display the data model on the command line
uicontrol('string','push me','position',[300 0 100 50], ...
'callback',@disp_data);
function disp_data(src,ev)
disp(data);
end
end
The basic uitable object in matlab is just a peer for the true table. To expose all the possible callbacks do the following
>> t = uitable;
>> tj = t.getTable;
>> set(tj)
gives you
ComponentHiddenCallback: string -or- function handle -or- cell array
ComponentMovedCallback: string -or- function handle -or- cell array
ComponentResizedCallback: string -or- function handle -or- cell array
ComponentShownCallback: string -or- function handle -or- cell array
MouseDraggedCallback: string -or- function handle -or- cell array
MouseMovedCallback: string -or- function handle -or- cell array
PropertyChangeCallback: string -or- function handle -or- cell array
FocusGainedCallback: string -or- function handle -or- cell array
FocusLostCallback: string -or- function handle -or- cell array
CaretPositionChangedCallback: string -or- function handle -or- cell array
InputMethodTextChangedCallback: string -or- function handle -or- cell array
KeyPressedCallback: string -or- function handle -or- cell array
KeyReleasedCallback: string -or- function handle -or- cell array
KeyTypedCallback: string -or- function handle -or- cell array
MouseClickedCallback: string -or- function handle -or- cell array
MouseEnteredCallback: string -or- function handle -or- cell array
MouseExitedCallback: string -or- function handle -or- cell array
MousePressedCallback: string -or- function handle -or- cell array
MouseReleasedCallback: string -or- function handle -or- cell array
HierarchyChangedCallback: string -or- function handle -or- cell array
VetoableChangeCallback: string -or- function handle -or- cell array
ComponentAddedCallback: string -or- function handle -or- cell array
ComponentRemovedCallback: string -or- function handle -or- cell array
AncestorMovedCallback: string -or- function handle -or- cell array
AncestorResizedCallback: string -or- function handle -or- cell array
AncestorMovedCallback_: string -or- function handle -or- cell array
AncestorAddedCallback: string -or- function handle -or- cell array
AncestorRemovedCallback: string -or- function handle -or- cell array
MouseWheelMovedCallback: string -or- function handle -or- cell array
ButtonDownFcn: string -or- function handle -or- cell array
You add the callback like
set(tj,'MouseClickedCallback',@(handle,event)disp('hello world'));
The handle is argument to the callback is the handle to the uitable and the event is a subclass of java.awt.event.
|
« Earlier | 9 items total |
Later » |
|
|