"Too few input arguments"? (GUI handles)

5 次查看(过去 30 天)
Hello helpful users and professionals,
I have the misfortune of creating a GUI without any knowledge of how to do so. After watching the videos and summaries I just can't wrap my head around transporting handles between functions.
Below is my current code that I have. I commented it into submission so it would perform a very basic operation of updating the figure window by making the left and right patches (dark brown segments) slowly reduce in size from top to bottom. It works as written if I copy and paste each function into the command window, but clicking run shows (I assume) that the function 'refreshFIG' is not getting WALL_L and WALL_R from the function 'showFIGWINDOW'. Clicking RUN will get the error "Too few input arguments (line 115)" I assume the problem is the function is not getting the 'YData' from WALL_L or WALL_R. Any help one of you nice people can give me to correct this would be a huge assist as I am exhausted by this problem.
CURRENT WORK IS AS FOLLOWS:
function Canyon_Racer
close all
clear
clc
WIDTH_LIMIT=.005; %CONTROLS RATE OF WALL CLOSURE
DROP_COUNT=5;
CWALL_L1=0;
CWALL_L2=0;
CWALL_L3=1;
CWALL_L4=1;
CWALL_L5=0;
CWALL_L6=30;
CWALL_L7=30;
CWALL_L8=0;
CWALL_R1=14;
CWALL_R2=14;
CWALL_R3=16;
CWALL_R4=16;
CWALL_R5=0;
CWALL_R6=30;
CWALL_R7=30;
CWALL_R8=0;
CWALL_Y=30;
CLOSE=2;
CWALL_LMAX=1;
CWALL_RMIN=14;
CWALL_TMAX=30;
DELAY=.25;
POLY_NUM=1;
WALL_WIDTH=14;
gameEND=0; %causes game to intiate and continue
% TITLE = ['Welcome to CANYON RUN' 10 10 ' Out run the bandits on your tail to sruvive!' 10 10 '...but don' 10 10 'crash into the canyon walls or you' 10 10 're history' 10 10
% ' player 1: player 2:' 10 10 ' use (LEFT) and (RIGHT) arrow keys to stay between the walls' 10 10];
function showFIGWINDOW
scrsz = get(0,'ScreenSize');
fig = figure('Position',[0, 0, 1, 1]);
set(fig , 'Units', 'Normalized', 'OuterPosition', [0 0 .75 .75], 'color', [127 84 23]./255); %display full screen
% CanyonWallX_L = [CWALL_LMIN CWALL_LMAX];
% CanyonWallX_R = [CWALL_RMIN CWALL_RMAX];
subplot('position', [0 0 1 1])
axis([0 15 0 30])
hold all
WALL_L = patch([CWALL_L1 CWALL_L2 CWALL_L3 CWALL_L4], [CWALL_L5 CWALL_L6 CWALL_L7 CWALL_L8], [0 0 0], 'edgecolor', 'none');
WALL_R = patch([CWALL_R1 CWALL_R2 CWALL_R3 CWALL_R4], [CWALL_R5 CWALL_R6 CWALL_R7 CWALL_R8], [0 0 0], 'edgecolor', 'none');
set(WALL_L, 'FaceColor', [127 84 23]./255)
set(WALL_R, 'FaceColor', [127 84 23]./255)
set(gca, 'XTick', [],'YTick', [], 'color', [184 95 0]./255)
set(fig,'Name','Canyon Racer v1.0','NumberTitle','off', 'toolbar', 'none', 'menubar', 'none')
movegui(fig,'center');
RACER_handle = patch([5 6 5.5], [3 3 5.5], [0 0 0]);
%%%Check this for uicontrol stuff: http://www.mathworks.com/help/matlab/ref/uicontrol.html
TBX = uicontrol( 'style', 'edit', 'String', 'Hi', 'Position', [.5, .5, 100.5, 15.5] );%%%%%%%DR. V
%%%Check out the "KeyPressFcn/KeyReleaseFcn portion of this:
%%%http://www.mathworks.com/help/matlab/ref/figure_props.html
set(fig, 'KeyPressFcn', {@KeyPressed, TBX, RACER_handle} ) %%%%%%%DR. V
set(fig, 'KeyReleaseFcn', {@KeyReleased, TBX}) %%%%%%%DR. V
end
function newGame
end
function KeyPressed( src, event, TBX, RACER_handle) %%%%%%%DR. V
set( TBX, 'String', sprintf('%s Pressed', event.Key ) );%%%%%%%DR. V
if strcmp(event.Key, 'leftarrow')
MOVE_handle = get(RACER_handle,'XData');
set(RACER_handle, 'XData', MOVE_handle-.1 );
elseif strcmp(event.Key, 'rightarrow')
MOVE_handle = get(RACER_handle,'XData');
set(RACER_handle, 'XData', MOVE_handle+.1 );
end
end %%%%%%%DR. V
function KeyReleased( src, event, TBX) %%%%%%%DR. V
set( TBX, 'String', sprintf('%s Released', event.Key ) );%%%%%%%DR. V
end %%%%%%%DR. V
function refreshFIG( src, event, WALL_L, WALL_R)
pause(DELAY)
SPEED_MOVE=1;
% if DROP_COUNT==5
%
% CWALL_L3=randi(CLOSE);
% while CWALL_L3>CLOSE
% CWALL_L3=randi(CLOSE);
% end
% if POLY_NUM==0
% WALL_L = patch([0 0 CWALL_L3 CWALL_L4], [CWALL_Y CWALL_Y+5 CWALL_Y+5 CWALL_Y], [0 0 0], 'edgecolor', 'none');
% WALL_R = patch([CWALL_L4+WALL_WIDTH CWALL_L3+WALL_WIDTH 15 15], [CWALL_Y CWALL_Y+5 CWALL_Y+5 CWALL_Y], [0 0 0], 'edgecolor', 'none');
% set(WALL_L, 'FaceColor', [127 84 23]./255)
% set(WALL_R, 'FaceColor', [127 84 23]./255)
% CWALL_L4=CWALL_L3;
% elseif POLY_NUM>0
% WALL_L = patch([0 0 CWALL_L3 CWALL_L4], [CWALL_Y CWALL_Y+5 CWALL_Y+5 CWALL_Y], [0 0 0], 'edgecolor', 'none');
% WALL_R = patch([CWALL_L4+WALL_WIDTH CWALL_L3+WALL_WIDTH 15 15], [CWALL_Y CWALL_Y+5 CWALL_Y+5 CWALL_Y], [0 0 0], 'edgecolor', 'none');
% set(WALL_L, 'FaceColor', [127 84 23]./255)
% set(WALL_R, 'FaceColor', [127 84 23]./255)
% CWALL_L4=CWALL_L3;
% end
% if POLY_NUM==6
% POLY_NUM=1;
% end
%
% end
DROP_WALL_L = get(WALL_L, 'YData');
set(WALL_L, 'YData', DROP_WALL_L-SPEED_MOVE)
DROP_WALL_R = get(WALL_R, 'YData');
set(WALL_R, 'YData', DROP_WALL_R-SPEED_MOVE)
% for NUM=(1:POLY_NUM)
% DROP_WALL_L = get(WALL_L(NUM), 'YData');
% set(WALL_L(NUM), 'YData', DROP_WALL_L-SPEED_MOVE)
% DROP_WALL_R = get(WALL_R(NUM), 'YData');
% set(WALL_R(NUM), 'YData', DROP_WALL_R-SPEED_MOVE)
% end
POLY_NUM=POLY_NUM+1;
% DROP_COUNT=DROP_COUNT+1;
% CWALL_LMAX=CWALL_LMAX+WIDTH_LIMIT; %LEFT WALL CLOSING IN
% CWALL_RMIN=CWALL_RMIN-WIDTH_LIMIT; %RIGHT WALL CLOSING IN
% WALL_L = patch([0 CWALL_LMAX], [CWALL_TMAX CWALL_TMAX]); %POST TO FIG WINDOW
% WALL_R = patch([CWALL_RMIN, 15], [CWALL_TMAX CWALL_TMAX]); %POST TO FIG WINDOW
% set(WALL_L, 'FaceColor', [127 84 23]./255) %FIG WALL AREA COLOR
% set(WALL_R, 'FaceColor', [127 84 23]./255) %FIG WALL AREA COLOR
end
%Game Primary Script
showFIGWINDOW
newGame
while gameEND==0
% moveWall;
refreshFIG;
% checkWALL;
end
% close(fig);
%
%
end

采纳的回答

Walter Roberson
Walter Roberson 2014-4-13
Your "Game Primary Script" has
refreshFIG;
which is a call to refreshFIG passing in no arguments. But you have defined refreshFIG as taking four arguments.
When you have a line such as
function refreshFIG( src, event, WALL_L, WALL_R)
then it does not mean "look around and find an execution-time value for each of the variables named and allow the current value to be used.
What it does mean is that the calling routine must pass in several values when it makes the call, and whatever value is passed in to the routine is to be given the temporary variable name inside the routine by matching the name positions in the "function" statement to the position of the value in the call to the function.
When a name is declared in a "function" statement argument list, then inside that routine, the correspondence between name and calling-position value overrides all other uses of that variable name that might otherwise have existed. Overrides use of the name as a MATLAB built-in function; overrides any fetching of the variable from any nested scope; overrides use of the name as indicating a class. In theory if you were to have a dummy argument name that was the same as the internal name of one of the operators, calling the operator would invoke the function handle passed in. For example if you were to use "minus" as an argument name, then in theory a-b would call that minus() instead of the normal builtin minus(a,b) that would otherwise be called. Therefore if you have a name that occurs in the argument list of your current function, then the only way for the named variable to have a value is if the function was called in a way that supplied an argument to that position.
  1 个评论
Chris Alkire
Chris Alkire 2014-4-21
I was in the mindset that I had to use handles and callbacks, but this is much easier. Thank you.

请先登录,再进行评论。

更多回答(2 个)

Chris Alkire
Chris Alkire 2014-4-13
Walter, thank you for taking the time to respond to my question. I am familiar with function arguments and you make a good point. If you look at the function showFIGWINDOW, towards the end 'RACER_handle' is called (or set) to be in functions 'press button' and 'release button' which sends the handle to those functions without having to have outputs in showFIGWINDOW. What I mean is:
USING --> set(fig, 'KeyReleaseFcn', {@KeyReleased, TBX})
avoids using this:
function TBX = showFIGWINDOW
where TBX is the output value to be imported into 'refreshFIG' as:
function refreshFIG(TBX)
What I am trying to do is determine how to get WALL_L and WALL_R (created in showFIGWINDOW) into refreshFIG in some fashion like running the following toward the end of showFIGWINDOW:
set(fig, '?????????', {@refreshFIG, WALL_L})
set(fig, '?????????', {@refreshFIG, WALL_R})
But I don't know what will work to do so which I believe will solve the error message.
Again, Walter, I appreciate you taking the time. Your response is spot on.

essrra
essrra 2023-3-4
function [predicted_labels,nn_index,accuracy] = KNN_(k,data,labels,t_data,t_labels)
%KNN_: classifying using k-nearest neighbors algorithm. The nearest neighbors
%search method is euclidean distance
%Usage:
% [predicted_labels,nn_index,accuracy] = KNN_(3,training,training_labels,testing,testing_labels)
% predicted_labels = KNN_(3,training,training_labels,testing)
%Input:
% - k: number of nearest neighbors
% - data: (NxD) training data; N is the number of samples and D is the
% dimensionality of each data point
% - labels: training labels
% - t_data: (MxD) testing data; M is the number of data points and D
% is the dimensionality of each data point
% - t_labels: testing labels (default = [])
%Output:
% - predicted_labels: the predicted labels based on the k-NN
% algorithm
% - nn_index: the index of the nearest training data point for each training sample (Mx1).
% - accuracy: if the testing labels are supported, the accuracy of
% the classification is returned, otherwise it will be zero.
%Author: Mahmoud Afifi - York University
%checks
if nargin < 4
error('Too few input arguments.')
elseif nargin < 5
t_labels=[];
accuracy=0;
end
if size(data,2)~=size(t_data,2)
error('data should have the same dimensionality');
end
if mod(k,2)==0
error('to reduce the chance of ties, please choose odd k');
end
%initialization
predicted_labels=zeros(size(t_data,1),1);
ed=zeros(size(t_data,1),size(data,1)); %ed: (MxN) euclidean distances
ind=zeros(size(t_data,1),size(data,1)); %corresponding indices (MxN)
k_nn=zeros(size(t_data,1),k); %k-nearest neighbors for testing sample (Mxk)
%calc euclidean distances between each testing data point and the training
%data samples
for test_point=1:size(t_data,1)
for train_point=1:size(data,1)
%calc and store sorted euclidean distances with corresponding indices
ed(test_point,train_point)=sqrt(...
sum((t_data(test_point,:)-data(train_point,:)).^2));
end
[ed(test_point,:),ind(test_point,:)]=sort(ed(test_point,:));
end
%find the nearest k for each data point of the testing data
k_nn=ind(:,1:k);
nn_index=k_nn(:,1);
%get the majority vote
for i=1:size(k_nn,1)
options=unique(labels(k_nn(i,:)'));
max_count=0;
max_label=0;
for j=1:length(options)
L=length(find(labels(k_nn(i,:)')==options(j)));
if L>max_count
max_label=options(j);
max_count=L;
end
end
predicted_labels(i)=max_label;
end
%calculate the classification accuracy
if isempty(t_labels)==0
accuracy=length(find(predicted_labels==t_labels))/size(t_data,1);
endfunction [predicted_labels,nn_index,accuracy] = KNN_(k,data,labels,t_data,t_labels)
%KNN_: classifying using k-nearest neighbors algorithm. The nearest neighbors
%search method is euclidean distance
%Usage:
% [predicted_labels,nn_index,accuracy] = KNN_(3,training,training_labels,testing,testing_labels)
% predicted_labels = KNN_(3,training,training_labels,testing)
%Input:
% - k: number of nearest neighbors
% - data: (NxD) training data; N is the number of samples and D is the
% dimensionality of each data point
% - labels: training labels
% - t_data: (MxD) testing data; M is the number of data points and D
% is the dimensionality of each data point
% - t_labels: testing labels (default = [])
%Output:
% - predicted_labels: the predicted labels based on the k-NN
% algorithm
% - nn_index: the index of the nearest training data point for each training sample (Mx1).
% - accuracy: if the testing labels are supported, the accuracy of
% the classification is returned, otherwise it will be zero.
%Author: Mahmoud Afifi - York University
%checks
if nargin < 4
error('Too few input arguments.')
elseif nargin < 5
t_labels=[];
accuracy=0;
end
if size(data,2)~=size(t_data,2)
error('data should have the same dimensionality');
end
if mod(k,2)==0
error('to reduce the chance of ties, please choose odd k');
end
%initialization
predicted_labels=zeros(size(t_data,1),1);
ed=zeros(size(t_data,1),size(data,1)); %ed: (MxN) euclidean distances
ind=zeros(size(t_data,1),size(data,1)); %corresponding indices (MxN)
k_nn=zeros(size(t_data,1),k); %k-nearest neighbors for testing sample (Mxk)
%calc euclidean distances between each testing data point and the training
%data samples
for test_point=1:size(t_data,1)
for train_point=1:size(data,1)
%calc and store sorted euclidean distances with corresponding indices
ed(test_point,train_point)=sqrt(...
sum((t_data(test_point,:)-data(train_point,:)).^2));
end
[ed(test_point,:),ind(test_point,:)]=sort(ed(test_point,:));
end
%find the nearest k for each data point of the testing data
k_nn=ind(:,1:k);
nn_index=k_nn(:,1);
%get the majority vote
for i=1:size(k_nn,1)
options=unique(labels(k_nn(i,:)'));
max_count=0;
max_label=0;
for j=1:length(options)
L=length(find(labels(k_nn(i,:)')==options(j)));
if L>max_count
max_label=options(j);
max_count=L;
end
end
predicted_labels(i)=max_label;
end
%calculate the classification accuracy
if isempty(t_labels)==0
accuracy=length(find(predicted_labels==t_labels))/size(t_data,1);
endfunction [predicted_labels,nn_index,accuracy] = KNN_(k,data,labels,t_data,t_labels)
%KNN_: classifying using k-nearest neighbors algorithm. The nearest neighbors
%search method is euclidean distance
%Usage:
% [predicted_labels,nn_index,accuracy] = KNN_(3,training,training_labels,testing,testing_labels)
% predicted_labels = KNN_(3,training,training_labels,testing)
%Input:
% - k: number of nearest neighbors
% - data: (NxD) training data; N is the number of samples and D is the
% dimensionality of each data point
% - labels: training labels
% - t_data: (MxD) testing data; M is the number of data points and D
% is the dimensionality of each data point
% - t_labels: testing labels (default = [])
%Output:
% - predicted_labels: the predicted labels based on the k-NN
% algorithm
% - nn_index: the index of the nearest training data point for each training sample (Mx1).
% - accuracy: if the testing labels are supported, the accuracy of
% the classification is returned, otherwise it will be zero.
%Author: Mahmoud Afifi - York University
%checks
if nargin < 4
error('Too few input arguments.')
elseif nargin < 5
t_labels=[];
accuracy=0;
end
if size(data,2)~=size(t_data,2)
error('data should have the same dimensionality');
end
if mod(k,2)==0
error('to reduce the chance of ties, please choose odd k');
end
%initialization
predicted_labels=zeros(size(t_data,1),1);
ed=zeros(size(t_data,1),size(data,1)); %ed: (MxN) euclidean distances
ind=zeros(size(t_data,1),size(data,1)); %corresponding indices (MxN)
k_nn=zeros(size(t_data,1),k); %k-nearest neighbors for testing sample (Mxk)
%calc euclidean distances between each testing data point and the training
%data samples
for test_point=1:size(t_data,1)
for train_point=1:size(data,1)
%calc and store sorted euclidean distances with corresponding indices
ed(test_point,train_point)=sqrt(...
sum((t_data(test_point,:)-data(train_point,:)).^2));
end
[ed(test_point,:),ind(test_point,:)]=sort(ed(test_point,:));
end
%find the nearest k for each data point of the testing data
k_nn=ind(:,1:k);
nn_index=k_nn(:,1);
%get the majority vote
for i=1:size(k_nn,1)
options=unique(labels(k_nn(i,:)'));
max_count=0;
max_label=0;
for j=1:length(options)
L=length(find(labels(k_nn(i,:)')==options(j)));
if L>max_count
max_label=options(j);
max_count=L;
end
end
predicted_labels(i)=max_label;
end
%calculate the classification accuracy
if isempty(t_labels)==0
accuracy=length(find(predicted_labels==t_labels))/size(t_data,1);
end it gives me error says
too few input arguements
  1 个评论
Walter Roberson
Walter Roberson 2023-3-4
You posted two different definitions for the same function KNN_, with the first definition being code in another programming language whose purpose for existing is to bankrupt Mathworks. (Not kidding.)
How are you invoking the matlab code?

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by