输入参数太多

时间:2015-08-10 21:22:27

标签: matlab printf

我在fprintf函数中收到有关太多输入参数的以下错误消息。但在我看来,只有适量的论点才得以通过。

这一切都在我制作的指南GUI的背景下(见最后的图片)。

Error while evaluating uicontrol Callback

calibration button hit
    20

   200

10
10
        2520

       25197

2520
25197
    'C0 2520 25197 10 10'

Error using serial/fprintf (line 115)
Too many input arguments.

Error in UserInterface>StaticCalibrationBtn_Callback (line 202)
fprintf(handles.s, 'C0 %s %s %s %s',StartStepsStr,EndStepsStr,Increment,Wait); 

这是代码

function StaticCalibrationBtn_Callback(hObject, eventdata, handles)
    % hObject    handle to StaticCalibrationBtn (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    disp('calibration button hit');
    Start = str2double(get(handles.CalFromUserTxt, 'string')); % Fetches the user inputed start location in mm and converts to double
    disp(Start);
    End = str2double(get(handles.CalToUserTxt, 'string')); % same for End position
    disp(End);
    Increment = get(handles.CalUserIncrementTxt, 'string'); % fetches the increment user inputed data as a string
    disp(Increment);
    Wait = get(handles.CalUserSpeedTxt, 'string'); % fetches the wait inputed data as a string
    disp(Wait);
    StartSteps = round(Start/0.00793750000); % computes the starting step position,double division
    disp(StartSteps);
    handles.StartSteps = StartSteps; % creats a place for the start steps inside the handles structure, to be fetched by anythingelsest be saved with guidata(hObject,handles)
    EndSteps = round(End/0.00793750000); % computes the end step position
    disp(EndSteps);
    handles.EndSteps = EndSteps; % stores the end steps to be accessed by anything else must be saved with guidata(hObject,handles)
    StartStepsStr = num2str(StartSteps); % converts the StartSteps double into a string so it can be sent over serial as a string
    disp(StartStepsStr);
    EndStepsStr = num2str(EndSteps); % converts the EndSteps double into a string so it can be sent over serial as a string
    disp(EndStepsStr);
    OutputString = strcat('C0' , {' '} , StartStepsStr , {' '} , EndStepsStr , {' '} , Increment , {' '} , Wait);
    disp(OutputString);
    fprintf(handles.s, 'C0 %s %s %s %s',StartStepsStr,EndStepsStr,Increment,Wait);

和handles.s来自

function SerialBtn_Callback(hObject, eventdata, handles)
% hObject    handle to SerialBtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA
comPort = get(handles.COMportTxt,'String');
if(~exist('serialFlag','var'))
    [handles.s, handles.serialFlag] = setupSerial(comPort);
end
guidata(hObject,handles);
end

和setupserial funciton

function [ s, flag] = setupSerial(comPort)
%Initialize serial port communication between Arduino and Matlab
%Ensure that the arduino is also communicating with Matlab at this time. 
%if setup is complete then the value of setup is returned as 1 else 0.

flag =1;
s = serial(comPort);
set(s,'DataBits',8);
set(s,'StopBits',1);
set(s,'BaudRate',9600);
set(s,'Parity','none');
fopen(s);
a='b';
while (a~='a')
    a=fread(s,1,'uchar');
end
if (a=='a')
    disp('serial read');
end
fprintf(s,'%c','a');
mbox = msgbox('Serial Communication setup.'); uiwait(mbox);
fscanf(s,'%u');
end

enter image description here

使用以下案件解决问题

OutputString = sprintf('C0 %s %s %s %s',StartStepsStr,EndStepsStr,Increment,Wait);
fprintf(handles.s,'%s', OutputString);

1 个答案:

答案 0 :(得分:2)

有多个函数叫做fprintf,一个用于文件,一个用于串行对象,还有一些用于其他函数。您正在使用功能从fprintf中获知文件,但您正在使用它与串行对象。查看可通过doc serial/fprintf

访问的正确文档