1
votes

I get the following error message about too many input arguments in my fprintf function. But it seems to me that just the right amount of arguments were passed.

All this is in the context of a guide GUI I made (see picture at the end).

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); 

Here is the code

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);

and where handles.s comes from

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

And the 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


USING THE FOLLOWING RESOLVED THE ISSUE

OutputString = sprintf('C0 %s %s %s %s',StartStepsStr,EndStepsStr,Increment,Wait);
fprintf(handles.s,'%s', OutputString);
1
Please, you cannot multiply purely "debugging" questions where you only change one line or the other comparing to your other question. In your case, try fprintf(handles.s, 'C0 %s %s %s %s',[StartStepsStr,EndStepsStr,Increment,Wait]);. And as Daniel said, read the right documentation for the function you are using : fprintf - Hoki
I thought Stack Exchange was based on a Question basis, not issue basis. Given the problem is a new one, I thought it was unrelated to my first question. Thanks for your input, I didnt know there were different functions based on the context it is used in, that is why I posted my question. I have gone through the documentation serial fprintf and your suggestion should fix my issue. - Ethienne
@Hoki. Unfortunetly, formating the arguments as you suggested did not work. The documentation lacks enough examples for me to make sens of what is being explained. I decided to go another rout and use : OutputString = sprintf('C0 %s %s %s %s',StartStepsStr,EndStepsStr,Increment,Wait); disp(OutputString); fprintf(handles.s,'%s', OutputString); - Ethienne

1 Answers

2
votes

There are multiple functions called fprintf, one for files, one for serial objects and some others. You are using functionally which you know from the fprintf for files, but you are using it with a serial object. Check the right documentation which can be accessed via doc serial/fprintf