unusual problem with an unusual solution

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 comentarios

Oleg Komarov
Oleg Komarov el 6 de Sept. de 2011
If I run your MATLAB only code it doesn't hang.
mohammad
mohammad el 6 de Sept. de 2011
you mean with running the above in your MATLAB, it doesn't hang?
but please try without this commands:
NET.addAssembly('System.Speech');
speaker = System.Speech.Synthesis.SpeechSynthesizer();
speaker.Rate = 1;
speaker.Volume = 100;
speaker.Speak('');
Oleg Komarov
Oleg Komarov el 6 de Sept. de 2011
I tried w/o.
mohammad
mohammad el 6 de Sept. de 2011
but it hangs in other PC too, matlab2009a
Grzegorz Knor
Grzegorz Knor el 7 de Sept. de 2011
Matlab doesn't hang on my PC too.
Line close(fh) in waitinput.m just close invisible, auxiliary figure.

Iniciar sesión para comentar.

 Respuesta aceptada

Oleg Komarov
Oleg Komarov el 6 de Sept. de 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 comentarios

Grzegorz Knor
Grzegorz Knor el 7 de Sept. de 2011
If you have installed Java properly there should be no problem with Robot class.
What is the output of the command ver on your PC?
mohammad
mohammad el 7 de Sept. de 2011
so nice
in function m-file cant use 'rob = Robot;', what must i do?
when use in a function, warning is:??? Error while evaluating TimerFcn for timer 'timer-47'
Undefined variable "rob" or class "rob.keyPress".
mohammad
mohammad el 7 de Sept. de 2011
Dear Grzegorz
when i use this in a common m-file there is no problem and works but when i copy and paste exactly the same code in a function m-file it gives warning
Oleg Komarov
Oleg Komarov el 7 de Sept. de 2011
See edit.
mohammad
mohammad el 7 de Sept. de 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 el 7 de Sept. de 2011
Add within the while:
elseif isempty(out)
stop(t)
break
end

Iniciar sesión para comentar.

Más respuestas (2)

mohammad
mohammad el 7 de Sept. de 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 comentarios

Grzegorz Knor
Grzegorz Knor el 7 de Sept. de 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 el 7 de Sept. de 2011
thanks, i think i must change MATLAB version
your code is really nice and need no extra thing
i appreciate you for this

Iniciar sesión para comentar.

Grzegorz Knor
Grzegorz Knor el 7 de Sept. de 2011

0 votos

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 comentarios

Oleg Komarov
Oleg Komarov el 7 de Sept. de 2011
Edited to include in fcn.
mohammad
mohammad el 7 de Sept. de 2011
OK you are right
i have no problem with 2010b, 7.11
thanks a lot
mohammad
mohammad el 7 de Sept. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Preguntada:

el 6 de Sept. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by