outputs in my function

2 views (last 30 days)
francisco
francisco on 27 Sep 2014
Edited: per isakson on 28 Sep 2014
Hello,Im need to use the output "a" in other function who calls testkeyboard, but when i do it, occurs the next error:
Output argument "rueda1" (and maybe others) not assigned during call to
this is the code of testkeyboard:
function [rueda1,rueda2,rueda3]=testkeyboard
figure('KeyPressFcn',@printfig);
function [rueda1,rueda2,rueda3] =printfig(~,event)
% Callback to parse keypress event data to print a figure
if event.Character == 'w'
rueda2=0;
rueda1=1;
rueda3=0;
elseif event.Character == 's'
rueda2=0;
rueda1=-1;
rueda3=0;
elseif event.Character == 'a'
rueda2=-1;
rueda1=0;
rueda3=0;
elseif event.Character == 'd'
rueda2=1;
rueda1=0;
rueda3=0;
elseif event.Character == 'i'
rueda2=0;
rueda1=0;
rueda3=1;
elseif event.Character == 'k'
rueda2=0;
rueda1=0;
rueda3=-1;
elseif event.Character == 'j'
rueda2=-1;
rueda1=-1;
rueda3=-1;
elseif event.Character == 'l'
rueda2=1;
rueda1=1;
rueda3=1;
else
rueda2=0;
rueda1=0;
rueda3=0;
end
disp(fprintf('rueda1= %d', rueda1));
disp(fprintf('rueda2= %d', rueda2));
disp(fprintf('rueda3= %d', rueda3));
end
end

Accepted Answer

per isakson
per isakson on 27 Sep 2014
Edited: per isakson on 28 Sep 2014
I made a couple of changes to you function and tested.
  • "Output argument "rueda1" (and maybe others) not assigned during call" &nbsp the reason is that the function executes to the end before you hit any key. One way to avoid that error is to add [rueda1,rueda2,rueda3] = deal( nan ); % <<<<<<<<< edit
  • prinfig is a nested function. It shares the variables rueda1,rueda2,rueda3 with its parent function. Thus function printfig(~,event) % <<<<<<<<< edit without output arguments
Test:
>> [rueda1,rueda2,rueda3]=testkeyboard;
w
rueda1= 1 9
rueda2= 0 9
rueda3= 0 9
k
rueda1= 0 9
rueda2= 0 9
rueda3= -1 10
>> [rueda1,rueda2,rueda3]
ans =
NaN NaN NaN
>>
where
function [rueda1,rueda2,rueda3]=testkeyboard
figure('KeyPressFcn',@printfig);
[rueda1,rueda2,rueda3] = deal( nan ); % <<<<<<<<< edit
function printfig(~,event) % <<<<<<<<< edit
% Callback to parse keypress event data to print a figure
if event.Character == 'w'
rueda2=0;
rueda1=1;
rueda3=0;
elseif event.Character == 's'
...
end
disp( event.Character ) % <<<<<<<<< edit
disp(fprintf('rueda1= %d', rueda1));
&nbsp
Regarding comment 1
"but they are always in NaN" &nbsp Yes that is the expected behavior. The function, testkeyboard, is run once and returns [nan,nan,nan] to the base (/caller) workspace. In response to pressing a key the nested function printfig is executed. It updates the values in the workspace of testkeyboard, but that's all. Adding the lines
assignin( 'caller', 'rueda1', rueda1 )
assignin( 'caller', 'rueda2', rueda2 )
assignin( 'caller', 'rueda3', rueda3 )
at the end of printfig will update the variable values in the base workspace. However, I don't think that helps.
"create a figure of a "robot", and move it with testkeyboard"
IMO: You need to step back and do some basic design. There is hardly a way to achieve the goal by small changes to your code.
Model-View-Controller is an ambitious approach. (There exists variants and different meaning of the term.) See
A nested-function-approach
function my_robot
rueda = nan(3,1); % shared variable
create_view
function create_view
figure('KeyPressFcn',@catch_keystroke);
end
function update_view
end
function catch_keystroke
update_view
end
end
  1 Comment
francisco
francisco on 28 Sep 2014
Edited: per isakson on 28 Sep 2014
thank you, that was very usefull, but i need to change the value of rueda1,rueda2 and rueda3, but they are always in NaN. this is the program caller of testkeyboard:
This create a figure of a "robot", and move it with testkeyboard
can you help me? Thanks
% x = 0;
y = 0;
tita = 0;
samplingTime = 0.1;
R = 0.15;
r = 0.05;
%figure(1), hold on, grid on
[a,b,c]= testkeyboard;
xlabel('X (metros)');
ylabel('Y (metros)');
xlim([-5 10]);
ylim([-5 10]);
legend
for cont = 1:inf
robot = hexarobo(x,y,tita);
rueda1=a; rueda2=b; rueda3=c;
B=[ -sin(tita + 1.570796327) cos(tita + 1.570796327) R;
-sin(tita + 3.665191429) cos(tita + 3.665191429) R;
-sin(tita + 5.759586532) cos(tita + 5.759586532) R];
x = x + r*B(1,:)*[rueda1; rueda2; rueda3];
y = y + r*B(2,:)*[rueda1; rueda2; rueda3];
tita = tita + r*B(3,:)*[rueda1; rueda2; rueda3];
pause(samplingTime); %Tiempo de pausa obligatorio
delete(robot); %borro objeto "robot" para ...con movimiento
end

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!