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

Oleg Komarov
Oleg Komarov 2011년 9월 6일
If I run your MATLAB only code it doesn't hang.
mohammad
mohammad 2011년 9월 6일
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 2011년 9월 6일
I tried w/o.
mohammad
mohammad 2011년 9월 6일
but it hangs in other PC too, matlab2009a
Grzegorz Knor
Grzegorz Knor 2011년 9월 7일
Matlab doesn't hang on my PC too.
Line close(fh) in waitinput.m just close invisible, auxiliary figure.

댓글을 달려면 로그인하십시오.

 채택된 답변

Oleg Komarov
Oleg Komarov 2011년 9월 6일

1 개 추천

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

Grzegorz Knor
Grzegorz Knor 2011년 9월 7일
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 2011년 9월 7일
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 2011년 9월 7일
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 2011년 9월 7일
See edit.
mohammad
mohammad 2011년 9월 7일
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 2011년 9월 7일
Add within the while:
elseif isempty(out)
stop(t)
break
end

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

mohammad
mohammad 2011년 9월 7일

0 개 추천

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

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

댓글을 달려면 로그인하십시오.

Grzegorz Knor
Grzegorz Knor 2011년 9월 7일

0 개 추천

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

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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by