MatLab strcat和fprintf函数

时间:2015-08-10 18:17:52

标签: matlab printf

我正在尝试使用fprintf函数在串行上发送一串字符。

我在MatLab GUIDE回调函数

中执行此操作

出于某种原因,fprintf不会将变量作为字符串传递给它,尽管在disp()函数调用中只将其作为上述一行处理。我想了解为什么会这样,我应该改变什么。

我从MatLab收到错误:

Error while evaluating uicontrol Callback

calibration button hit
    10

   200

30
100
        1260

       25197

1260
25197
    'C0 1260 25197 30 100'

Error using serial/fprintf (line 84)
The third input argument must be a string.

Error in UserInterface>StaticCalibrationBtn_Callback (line 202)
fprintf(handles.s, '%s', OutputString);

以下是导致问题的代码示例

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, '%s', OutputString);

导致问题的fprintf调用位于代码snipet的底部。 在通话之前成功建立了串行通信。

有关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

该功能本身如下所述

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

结束

编辑:有人指出strcat函数输出一个字符串数组而不是一个字符串,它与fprintf不兼容。那么问题是:

如何使用strcat生成一个包含与fprintf函数兼容的空格的字符串?

编辑2: 以下

space = ' ';
OutputString = strcat('C0' , space , StartStepsStr , space , EndStepsStr , space , Increment , space , Wait);

输出以下内容,没有'' ...所以我们慢慢到达那里。

C01260251972020

0 个答案:

没有答案