Info
この質問は閉じられています。 編集または回答するには再度開いてください。
Get data from a Cellstr
3 ビュー (過去 30 日間)
古いコメントを表示
Hi
I have a Cellstr and I want to call the data from it 5 numbers at a time, so the user enters the text, and a binary value is stored in the cellstr depending on the ASKI value of the character entered, I want to call the binary values 5 at a time and use strrep to replce them with a character depending on the value of the binary number
Hopefully this makese sense!
Thanks
usertext = input('Enter your text message :','s');
usertext = usertext;
binary = cellstr(num2str(double(usertext) < 95));
3 件のコメント
回答 (1 件)
dpb
2019 年 10 月 22 日
On the presumption there's a much larger lookup table in the real application...
X=[17,21]; % lookup table independent value
Y=[1,2]; % output position for code
C=['AB']; % desired code
b=repelem('0',numel(txt));b(txt<95)='1'; % build the binary string from input
b=bin2dec(reshape(b,5,[]).'); % convert 5 bits to decimal
code=C(interp1(X,Y,b))
ans =
'AB'
>>
5 件のコメント
dpb
2019 年 10 月 23 日
編集済み: dpb
2019 年 10 月 23 日
X/Y are the lookup table -- the X is the numeric value associated with the Yth code index -- it's indirect because interp1 won't allow char() variables as the Y vector. b is the decimal value of the binary string created by the logical operation on the input string (in 5 bit lengths).
interp1 returns the Y associated with the input X as an array of indices into the C code characters...for robustness would want to ensure all the b exist in X (iow, the code is defined for all the inputs given. For length of 5, that's 2^5 elements.)
You could write it as character strings instead and use string operations, but it's more concise to convert those to numeric in the lookup table and then convert the user input as well.
MINOR CORRECTION: "ASKI" is "ASCII"
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!