When using inputdlg with str2double, Matlab hangs if a number is entered without a nonzero decimal
5 views (last 30 days)
Show older comments
Hello, I'm new to asking questions, but am pretty familiar with Matlab, and I have no idea why this is happening. I am writing a program that will take data from an IMU and calculate things like speed and angular position for my job, so the names of variables and comments have been modified slightly, because legal reasons. I'm using R2013a by the way.
In order to make the program consistent, I setup an automatic motion detection, but because of the object I'm measuring, the data has large portions of no motion. To keep my automatic detection, I instead implemented a dialogue box that prompts the user to input a time just before an event they want to analyze. To do this, I draw a fullscreen plot of the data from one of the gyroscopes, and make a pop-up window appear asking for input. This is where I have my problem:
%Semi-automatic movement detection, finds indices where the gyro reading crosses
%the threshold after the time specified by the user
fig=figure('Name','Event Locator','units','normalized','outerposition',[0 0 1 1]);
plot(time,xgyro);
xlabel('Time (s)');
ylabel('Angular Velocity (deg/s)');
title('Corrected Angular Velocities');
legend('Xgyro','Location','northwest');
%popup window asking for a time just before the event you want to analyze
prompt = 'Enter a time just before the event you want to analyze: ';
t=str2double(inputdlg([prompt,'Event Timing']));
drawnow;
pause(0.5);
waitfor(t);<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
close(fig);
All of the variables are defined in earlier m-files, and everything works perfectly if the number entered has a nonzero decimal (ie, 5.1,3.000000000001,2.9 - but not 7 or 6.0). If a nonzero decimal is entered the program continues on its merry way, but if it isn't then the dialog box closes, the figure doesn't close, and if I force the code to stop, it tells me the code was stopped on the line I indicated with the <<<<<.
Has anyone seen this before, or does anyone know why this is happening? I'd greatly appreciate any help I can get. I've tried to fix it with the ol "drawnow and pause" trick, but to no avail.
Cheers,
Conlan
2 Comments
Adam
on 30 Sep 2015
I tried the part of your code from with just opening a figure, running the prompt and closing it and it works fine for me with values like 7 or 6.0.
Accepted Answer
Stephen23
on 30 Sep 2015
Edited: Stephen23
on 30 Sep 2015
This waitfor call does not make any sense. The inputdlg documentation states "The returned variable answer is a cell array containing strings, one string per text entry field, starting from the top of the dialog box." When you input a cell array with 2tr2double, it "...converts the strings in the cell array of strings C to double precision". You then enter these double value/s into waitfor, but its documentation states that the first input must be a graphics object handle: "waitfor(h) blocks the caller from executing statements until the graphics object identified by h closes (is deleted)". You are not giving it a graphics handle, just some random number/s that the user has entered. It is possible, depending on MATLAB version, that the user might happen to enter a value that is the same as a graphics object handle, in which case waitfor will patiently wait until that graphics object is deleted. You might be lucky, you might not.
Rather than this:
waitfor(t)
perhaps you really intended to write this:
pause(t)
Where pause is described in the documentation as "pause(n) pauses execution for n seconds before continuing."
In any case t is not a graphics handle, so it cannot be used in waitfor like that.
2 Comments
Adam
on 30 Sep 2015
Edited: Adam
on 30 Sep 2015
That would definitely explain the symptoms, (I never bothered to look into the usage of waitfor and just blindly followed it!), but since figure handles were always integers (as doubles) I wouldn't be at all surprised if he had figures 6 and 7 open while running this.
More Answers (0)
See Also
Categories
Find more on Graphics Performance 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!