2015-08-10 5 views
1

Я получаю следующее сообщение об ошибке относительно слишком большого количества входных аргументов в моей функции 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); 
+0

Пожалуйста, вы не можете умножать чисто «отладочные» вопросы, когда вы меняете только одну строку или другую по сравнению с другими [вопрос] (http://stackoverflow.com/questions/ 31926428/Matlab-fprintf-функции). В вашем случае попробуйте 'fprintf (handles.s, 'C0% s% s% s% s', [StartStepsStr, EndStepsStr, Increment, Wait]);'. И, как сказал Дэниел, прочитайте правильную документацию для функции, которую вы используете: ['fprintf'] (http://mathworks.com/help/matlab/ref/serial.fprintf.html) – Hoki

+0

Я думал, что Stack Exchange основан на Основание вопроса, а не вопрос. Учитывая, что проблема новая, я думал, что она не связана с моим первым вопросом. Спасибо за ваш вклад, я не знал, что существуют разные функции, основанные на контексте, в котором он используется, поэтому я разместил свой вопрос. Я просмотрел документацию серийного fprintf, и ваше предложение должно исправить мою проблему. – Ethienne

+0

@Hoki. К сожалению, составление аргументов, как вы предложили, не сработало. В документации недостаточно примеров для того, чтобы понять, что объясняется. Я решил пойти еще раз и использовать: 'OutputString = sprintf ('C0% s% s% s% s', StartStepsStr, EndStepsStr, Increment, Wait); disp (OutputString); fprintf (handles.s, '% s', OutputString); ' – Ethienne

ответ

2

Есть несколько функций, вызываемых fprintf, один для файлов, один для серийных объектов и некоторых других. Вы используете функционально, что вы знаете из fprintf для файлов, но используете его с серийным объектом. Проверьте правильную документацию, доступ к которой можно получить через doc serial/fprintf