Prev: help
Next: Force-Feedback Joystick
From: Matt Smerud on 8 Jul 2010 11:24 Hey I'm relatively new to Matlab programming and I have this error I can't work out. I have this code as a the callback function for a slider in my GUI. sliderValue = get(handles.slider1,'Value') vecStr = ['[', num2str(handles.X(sliderValue)), ', ', num2str(handles.Y(sliderValue)), ', ', num2str(handles.Z(sliderValue)), ']']; set(handles.vector,'String', vecStr); set(handles.input,'String', round(num2str(sliderValue)*5*10^-5)); axes(handles.axes1); sliderValue = round(sliderValue) * 5 * 10^-5; [x y] = meshgrid(-10:10); z = (handles.X(sliderValue) .* x + handles.Y(sliderValue) .* y)./ -handles.Z(sliderValue); handles.current = z; surf(x,y,handles.current); axis([-10 10 -10 10 -3*10^-3 3*10^-3]); Basically the X, Y, and Z variables represent a vector which I am using to create a plane. I print the vector to a static text box and output the plane to my axes. My problem is that I recieve this error when defining vecStr: ??? Attempted to access handles.X(128752); index must be a positive integer or logical. This error only occurs at some points however which is what confuses me even more. Like it will work for 140000 but not 120000. Any help would be much appreciated. Thanks.
From: Jan Simon on 8 Jul 2010 11:51 Dear Matt, > vecStr = ['[', num2str(handles.X(sliderValue)), ', ', num2str(handles.Y(sliderValue)), ', ', num2str(handles.Z(sliderValue)), ']']; > ??? Attempted to access handles.X(128752); index must be a positive integer or logical. > > This error only occurs at some points however which is what confuses me even more. Like it will work for 140000 but not 120000. If I understand correctly, this fails: vecStr = ['[', num2str(handles.X(120000)), ', ', num2str(handles.Y(120000)), ', ', num2str(handles.Z(120000)), ']']; Are you absolutely sure, that there are no figures after the point as e.g. 128752.000000000001 Do you expect the value of the slider object to be integer? BTW. Slightly nicer: vecStr = sprintf(['%f, %f, %f], handles.X(n), handles.Y(n), handles.Z(n)) but this is just a question of taste. Good luck, Jan
From: Matt Smerud on 8 Jul 2010 12:05 > If I understand correctly, this fails: > vecStr = ['[', num2str(handles.X(120000)), ', ', > num2str(handles.Y(120000)), ', ', > num2str(handles.Z(120000)), ']']; > Are you absolutely sure, that there are no figures after the point as e.g. > 128752.000000000001 > Do you expect the value of the slider object to be integer? > > BTW. Slightly nicer: > vecStr = sprintf(['%f, %f, %f], handles.X(n), handles.Y(n), handles.Z(n)) > but this is just a question of taste. > > Good luck, Jan No it is not because of a decimal. I just tried adding a round command before that line and it still gives the same error.
From: Jan Simon on 8 Jul 2010 12:20 Dear Matt, > > vecStr = ['[', num2str(handles.X(120000)), ', ', > > num2str(handles.Y(120000)), ', ', > > num2str(handles.Z(120000)), ']']; > No it is not because of a decimal. I just tried adding a round command before that line and it still gives the same error. Then inspect it manually again. Get you struct "handles" from the GUI to the command line (e.g. with a KEYBOARD command before "vecStr = ..."). Now try this: class(handles.X) size(handles.X) class(sliderValue) handles.X(sliderValue) Another idea is "dbstop if error", which enables the debugger, when an error occurs. Then inspect the values of your variables. All you have shown seems to be correct. Is there something, you haven't posted? Kind regards, Jan
From: Matt Smerud on 8 Jul 2010 12:40
Ok I think I got it working again. I think it actually was the rounding thing. I missed that it was giving me an error for the text box callback and not the slider callback again so I just saw the error message said the same thing and assummed it hadn't worked. With the round command in both callbacks it works though. Thank you so much for your help. |