Matlab rejects string with single quotation even after using strrep

1 回表示 (過去 30 日間)
Matuno
Matuno 2013 年 12 月 4 日
コメント済み: Matuno 2013 年 12 月 4 日
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?

採用された回答

dpb
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 ExchangeEntering Commands についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by