unusual problem with an unusual solution

1 view (last 30 days)
i use these 2 codes:
i use these functions together, but after running matlab hangs. i fund that 'close' command in line '69' of waitinput.m caused this that while i delete this command, matlab not hangs but it cause its problems.
i found also an unusual solution is to put a command between cprintf.m calling and waitinput.m function. this commands is:
NET.addAssembly('System.Speech');
speaker = System.Speech.Synthesis.SpeechSynthesizer();
speaker.Rate = 1;
speaker.Volume = 100;
speaker.Speak('hello');
my program is:
clc,clear all
t=5;
cprintf('blue','in %ds type 1 to plot 3 signals in one figure', t);
sel=waitinput('>>',t);
if sel == 1;
figure, plot((1:10),'c')
hold on
plot((5:10),'k')
plot(rand(100,1),'r')
hold off;
legend('a','b','c')
end
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
NET.addAssembly('System.Speech');
speaker = System.Speech.Synthesis.SpeechSynthesizer();
speaker.Rate = 1;
speaker.Volume = 100;
speaker.Speak('');
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cprintf('blue','in %ds type 2 to plot 3 signals separately', t);
sel=waitinput('>>',t);
if sel == 2;
subplot(311)
plot(rand(100,1),'r')
subplot(312)
bar(rand(5,1),'c')
subplot(313)
semilogy(1:10)
end
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
NET.addAssembly('System.Speech');
speaker = System.Speech.Synthesis.SpeechSynthesizer();
speaker.Rate = 1;
speaker.Volume = 100;
speaker.Speak('');
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cprintf('g','**************************************************************************************')
fprintf(1,repmat('\n',1,1));
now when i delete this command between of these 2 (waitinput and cprintf) matlab hangs (because of 'close' command in line 69 of waitinput.m but i dont know for what). i need one of these:
1-finding cause of the problem and solving it
or
2-puting another command between cprintf and waitinput instead of 'NET.addAssembly('System.Speech');' because in some PCs the framework doesn't install.
please give me a hand in one of this way
  5 Comments
mohammad
mohammad on 6 Sep 2011
but it hangs in other PC too, matlab2009a
Grzegorz Knor
Grzegorz Knor on 7 Sep 2011
Matlab doesn't hang on my PC too.
Line close(fh) in waitinput.m just close invisible, auxiliary figure.

Sign in to comment.

Accepted Answer

Oleg Komarov
Oleg Komarov on 6 Sep 2011
An alternative to your code:
% Use Java's robot
import java.awt.*;
rob = Robot;
% Setup timer that will abort input in 5 seconds
sec = 5;
t = timer('StartDelay',sec,'TasksToExecute',1);
set(t,'TimerFcn',['rob.keyPress(java.awt.event.KeyEvent.VK_ENTER);'...
'rob.keyRelease(java.awt.event.KeyEvent.VK_ENTER);'])
% Start and run till timer executes it first call in 5 seconds
start(t)
while strcmp(get(t,'Running'),'on')
out = input(sprintf('In %ds type 1 to plot 3 signals: ',sec));
if out == 1
plot(1:10,1:10,'c',1:6,5:10,'k',1:100,rand(100,1),'r')
legend('a','b','c')
stop(t)
break
end
end
If you want to use cprintf then replace out = input... with:
cprintf('blue','In %ds type 1 to plot 3 signals: ',sec);
out = input('');
EDIT
Hardcoding every Java call in the TimerFcn:
function foo(sec)
t = timer('StartDelay',sec,'TasksToExecute',1);
set(t,'TimerFcn',['rob = java.awt.Robot;',...
'rob.keyPress(java.awt.event.KeyEvent.VK_ENTER);'...
'rob.keyRelease(java.awt.event.KeyEvent.VK_ENTER);'])
...
end
  6 Comments
mohammad
mohammad on 7 Sep 2011
thanks a lot, works perfect
only i wish that when it request typing 1 or 2 and its going countdown, by pushing 'enter' goes to next line (command) without continuing countdown
Oleg Komarov
Oleg Komarov on 7 Sep 2011
Add within the while:
elseif isempty(out)
stop(t)
break
end

Sign in to comment.

More Answers (2)

mohammad
mohammad on 7 Sep 2011
Dear Grzegorz do you have problem when running this :
clc,clear all
t=5;
cprintf('blue','in %ds type 1 to plot 3 signals in one figure', t);
sel=waitinput('>>',t);
if sel == 1;
figure, plot((1:10),'c')
hold on
plot((5:10),'k')
plot(rand(100,1),'r')
hold off;
legend('a','b','c')
end
cprintf('blue','in %ds type 2 to plot 3 signals separately', t);
sel=waitinput('>>',t);
if sel == 2;
subplot(311)
plot(rand(100,1),'r')
subplot(312)
bar(rand(5,1),'c')
subplot(313)
semilogy(1:10)
end
cprintf('g','**************************************************************************************')
fprintf(1,repmat('\n',1,1));
  2 Comments
Grzegorz Knor
Grzegorz Knor on 7 Sep 2011
nope... it's OK.
BTW, my output from ver:
MATLAB Version 7.10.0.499 (R2010a)
Operating System: Linux 2.6.38-11-generic-pae #48-Ubuntu SMP Fri Jul 29 20:51:21 UTC 2011 i686
Java VM Version: Java 1.6.0_12-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
mohammad
mohammad on 7 Sep 2011
thanks, i think i must change MATLAB version
your code is really nice and need no extra thing
i appreciate you for this

Sign in to comment.


Grzegorz Knor
Grzegorz Knor on 7 Sep 2011
OK. There is problem with rob.keyPress in Oleg Komarov answer. When the time comes to an end an error occurs. So if you want to use this solution you have to write function that supports this case.
But I still can not trace the error of waitinput.
  3 Comments
mohammad
mohammad on 7 Sep 2011
OK you are right
i have no problem with 2010b, 7.11
thanks a lot
mohammad
mohammad on 7 Sep 2011
thanks a lot Oleg, works perfect
only i wish that when it request typing 1 or 2 and its going countdown, by pushing 'enter' goes to next line (command) without continuing countdown

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!