unusual problem with an unusual solution
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
2011-9-6
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 个评论
更多回答(2 个)
mohammad
2011-9-7
2 个评论
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
Grzegorz Knor
2011-9-7
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!