Matlab rejects string with single quotation even after using strrep
1 次查看(过去 30 天)
显示 更早的评论
I am working with serial port and a simple GUI to send a particular string to the serial port. The value will change the destination address for Xbee. The code in a simpler way is like this:
s = serial ('COM6'); % the communication port
fopen(s); %open the serial port
set(s,'terminator','CR');
TxTex = get(handles.destAddMSB, 'string');%destAddMSB is a Edit text box in GUI where I am %inputting a string
disp(TxTex);
msb = strcat('ATDH', TxTex);
fwrite(s,'+++');
pause(1);
status = fscanf(s);
index=strfind(status,'OK');
fprintf(s,'%s',msb);
pause(0.1);
The Error message Matlab is showing:
The third input argument must be a string.
whereas the third input is msb in above mentioned code. I tried it otherway like sending only a string instead of msb and it is working ok. The main problem is when I am displaying msb it is showing in a single quotation [e.g. 'ATDH13A200']. Whereas for successful case there should not be any single quotation [i.e. ATDH13A200]. I have tried to use strrep(msb, char(39), ''); but it shows the same quotation mark. Could anyone please let me know how to accomplish this task so that the msb would work like a real string without the quotation marks?
0 个评论
采纳的回答
dpb
2013-12-4
The single quotes when displaying a variable indicate the content of a cell array containing a character variable, not a character variable itself. Note the following demonstration--
>> s='ATHD'
s =
ATHD
>> s={'ATHD'}
s =
'ATHD'
>> char(s)
ans =
ATHD
>>
The answer is to use char to cast the the cell to the character string. Or, you can write
msb{:}
using the curlies to dereference the cell instead.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!