Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
JSlider events in a que

Subject: JSlider events in a que

From: Arwel Hughes

Date: 15 Nov, 2007 16:03:13

Message: 1 of 10

In the following, moving the JSlider calls the user function
as can be seen. But, if the function is slow (simulated
using the pause here), then its possible to create a whole
load of calls to the function before the first one has
finished. How can the queing of these be controlled. Foe
example, is it possible to ignore any calls occurring before
the first one has finished?
What is the 'best' way of dealing with a slider (or anything
else) making lots of rapid calls to a slow function?

function sltest()
import javax.swing.*;
import java.awt.*;

a = JFrame();
a.setSize(300,300);
b = JSlider(0,100,50);
set(b,'StateChangedCallback',@myFun);
a.getContentPane.add(b);
a.show

function myFun(src,ev)
pause(1)
disp('In the callback')

Subject: JSlider events in a que

From: Yair Altman

Date: 15 Nov, 2007 16:18:38

Message: 2 of 10

> What is the 'best' way of dealing with a slider (or anything
> else) making lots of rapid calls to a slow function?


Good question, with lots of theoretical analyses over the
years. Here's a skeleton of one particular method I found
easy to implement and highly effective in Matlab callbacks:

function myCallback(src,evt,varargin)
  try
    if ~isempty(getappdata(src,'InCallback')), return; end
    setappdata(src,'InCallback',1);
    % do whatever you need to do in the callback...
  catch
    % handle errors...
  end
  % Re-enable callback, after all processing/errors are over
  setappdata(src,'InCallback',[]);
end

Yair Altman
http://ymasoftware.com

Subject: JSlider events in a que

From: Arwel Hughes

Date: 15 Nov, 2007 18:33:03

Message: 3 of 10

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message
<fhhrgu$oej$1@fred.mathworks.com>...
> > What is the 'best' way of dealing with a slider (or anything
> > else) making lots of rapid calls to a slow function?
>
>
> Good question, with lots of theoretical analyses over the
> years. Here's a skeleton of one particular method I found
> easy to implement and highly effective in Matlab callbacks:
>
> function myCallback(src,evt,varargin)
> try
> if ~isempty(getappdata(src,'InCallback')), return; end
> setappdata(src,'InCallback',1);
> % do whatever you need to do in the callback...
> catch
> % handle errors...
> end
> % Re-enable callback, after all processing/errors are over
> setappdata(src,'InCallback',[]);
> end
>
> Yair Altman
> http://ymasoftware.com
>


Thanks Yair.
In the case of JSlider though, I found this post here which
may also be useful...

http://objectmix.com/java/73669-update-gui-while-jslider-moving.html

In Matlab, it turns out you can also do
get(src,'ValueIsAdjusting')
and do additional checking in the callback.
 

Subject: JSlider events in a que

From: Johannes

Date: 8 Feb, 2008 08:46:24

Message: 4 of 10

Hello,

I read your post and wondered how can I read out
'ValueIsAdjusting' I didn't find that property in the figure
nor in the '0' handle nor in the slider handle. So where can
i find this property?

Thanks for helping.

Kindest regards,
Johannes

Subject: JSlider events in a que

From: Arwel Hughes

Date: 8 Feb, 2008 09:56:07

Message: 5 of 10

Hi Johannes,
When you set the 'StateChangedCallback', the calback ends
up with two input parameters. This can be accessed from the
first one, like this.....(theres lots of useful stuff here.
Just do get(src) or get(ev) to see them)


s = JSlider(JSlider.VERTICAL,0,100,50);
set(s,'StateChangedCallback',@sliderCallback);







function sliderCallback(src,ev);

moving = get(src,'ValueIsAdjusting');
if (strcmp(moving,'off') == 1)
    return
end






"Johannes " <RudiRocker@gmx.net> wrote in message <foh4t0
$qat$1@fred.mathworks.com>...
> Hello,
>
> I read your post and wondered how can I read out
> 'ValueIsAdjusting' I didn't find that property in the
figure
> nor in the '0' handle nor in the slider handle. So where
can
> i find this property?
>
> Thanks for helping.
>
> Kindest regards,
> Johannes

Subject: JSlider events in a que

From: Johannes

Date: 8 Feb, 2008 10:19:03

Message: 6 of 10

Thanks for your reply, but how can I create a JSlider in an
existing gui?

Greetings,
Johannes

Subject: JSlider events in a que

From: Arwel Hughes

Date: 8 Feb, 2008 11:45:04

Message: 7 of 10

Hi Johannes,
JSlider is a Java component, so what I do is to put it into
a pure Java container (like a JFrame). So a very simple
example would be to do something like this.....


import javax.swing.*;
import java.awt.*;
myFrame = JFrame();
mySlider = JSlider();
myFrame.getContentPane().add(mySlider);
myFrame.setSize(300,300);
myFrame.setVisible(true);


If you want to use a Java component in a Matlab gui, you
have to use the 'javacomponent' command. You can find
details of that here..


http://www.mathworks.co.uk/matlabcentral/fileexchange/loadFi
le.do?objectId=15580&objectType=FILE

Hope this helps,
Arwel














"Johannes " <RudiRocker@gmx.net> wrote in message
<fohaan$dsg$1@fred.mathworks.com>...
> Thanks for your reply, but how can I create a JSlider in
an
> existing gui?
>
> Greetings,
> Johannes
>

Subject: JSlider events in a que

From: Vikram

Date: 14 Mar, 2008 04:31:01

Message: 8 of 10

"Arwel Hughes" <a.v.hughes@rl.ac.uk> wrote in message
<fhhqk1$bkh$1@fred.mathworks.com>...
> In the following, moving the JSlider calls the user function
> as can be seen. But, if the function is slow (simulated
> using the pause here), then its possible to create a whole
> load of calls to the function before the first one has
> finished. How can the queing of these be controlled. Foe
> example, is it possible to ignore any calls occurring before
> the first one has finished?
> What is the 'best' way of dealing with a slider (or anything
> else) making lots of rapid calls to a slow function?
>
> function sltest()
> import javax.swing.*;
> import java.awt.*;
>
> a = JFrame();
> a.setSize(300,300);
> b = JSlider(0,100,50);
> set(b,'StateChangedCallback',@myFun);
> a.getContentPane.add(b);
> a.show
>
> function myFun(src,ev)
> pause(1)
> disp('In the callback')


Hello, I read your post, I am doing something similar, the
problem is when I try to deploy the project using MATLAB
compiler, the deployment returns a message saying it's
successful, but when I try to run the exe file, the frame
/window that was created using java in MATLAB closes
instantly, how do I keep it open ?

Subject: JSlider events in a que

From: Arwel Hughes

Date: 14 Mar, 2008 10:13:02

Message: 9 of 10

"Vikram " <vikramthevictor@gmail.com> wrote in message
<frcv25$342$1@fred.mathworks.com>...
> "Arwel Hughes" <a.v.hughes@rl.ac.uk> wrote in message
> <fhhqk1$bkh$1@fred.mathworks.com>...
> > In the following, moving the JSlider calls the user
function
> > as can be seen. But, if the function is slow (simulated
> > using the pause here), then its possible to create a
whole
> > load of calls to the function before the first one has
> > finished. How can the queing of these be controlled. Foe
> > example, is it possible to ignore any calls occurring
before
> > the first one has finished?
> > What is the 'best' way of dealing with a slider (or
anything
> > else) making lots of rapid calls to a slow function?
> >
> > function sltest()
> > import javax.swing.*;
> > import java.awt.*;
> >
> > a = JFrame();
> > a.setSize(300,300);
> > b = JSlider(0,100,50);
> > set(b,'StateChangedCallback',@myFun);
> > a.getContentPane.add(b);
> > a.show
> >
> > function myFun(src,ev)
> > pause(1)
> > disp('In the callback')
>
>
> Hello, I read your post, I am doing something similar, the
> problem is when I try to deploy the project using MATLAB
> compiler, the deployment returns a message saying it's
> successful, but when I try to run the exe file, the frame
> /window that was created using java in MATLAB closes
> instantly, how do I keep it open ?



Hi Vikram,
I'm afraid I don't know much about the compiler. Maybe you
should ask your question in a new post? (I think there's a
specific mlab/java compiler toolbox available, for example).
Cheers,
Arwel

Subject: JSlider events in a que

From: Steven Lord

Date: 14 Mar, 2008 13:45:15

Message: 10 of 10


"Vikram " <vikramthevictor@gmail.com> wrote in message
news:frcv25$342$1@fred.mathworks.com...
> "Arwel Hughes" <a.v.hughes@rl.ac.uk> wrote in message
> <fhhqk1$bkh$1@fred.mathworks.com>...
>> In the following, moving the JSlider calls the user function
>> as can be seen. But, if the function is slow (simulated
>> using the pause here), then its possible to create a whole
>> load of calls to the function before the first one has
>> finished. How can the queing of these be controlled. Foe
>> example, is it possible to ignore any calls occurring before
>> the first one has finished?
>> What is the 'best' way of dealing with a slider (or anything
>> else) making lots of rapid calls to a slow function?
>>
>> function sltest()
>> import javax.swing.*;
>> import java.awt.*;
>>
>> a = JFrame();
>> a.setSize(300,300);
>> b = JSlider(0,100,50);
>> set(b,'StateChangedCallback',@myFun);
>> a.getContentPane.add(b);
>> a.show
>>
>> function myFun(src,ev)
>> pause(1)
>> disp('In the callback')
>
>
> Hello, I read your post, I am doing something similar, the
> problem is when I try to deploy the project using MATLAB
> compiler, the deployment returns a message saying it's
> successful, but when I try to run the exe file, the frame
> /window that was created using java in MATLAB closes
> instantly, how do I keep it open ?

I think you may be encountering the same type of situation as the poster in
this thread:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/162567#411123

Try what I suggested to that poster and see if it does what you want.

--
Steve Lord
slord@mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us