Get data from a Cellstr

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

dpb
dpb 2019년 10월 22일
편집: dpb 2019년 10월 22일
"Hopefully this makes sense!"
Well, not really. Maybe if spent 30 minutes trying to parse what you wrote, but...
Gives us some examples of expected input/desired output...
Sorry! My code
usertext = input('Enter your text message :','s');
usertext = usertext;
binary = cellstr(num2str(double(usertext) < 95));
Takes the usertext input and based if the ASKI value of the character is above 95 places a binary value in a cellstr.
I want to use the binary values (in blocks of five) , to use something like Strrep to replace the binary numbers with letters based on a code EG:
10001 = A
10101 = B
So if I entered 'TestINgOnE' my Cellstr would store : 1 0 0 0 1 1 0 1 0 1
and based on my key the output would be AB
Hopefully that makes abit more sense.
What about this :
usertext = input('Enter your text message :','s');
usertext = usertext;
binary =(num2str(double(usertext) < 95));
str = binary;
j = 1;
for k = 1: 3: length(str)
word = strread(str,'%3s')
j = j+1;
end

답변 (1개)

dpb
dpb 2019년 10월 22일

0 개 추천

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

James Knight
James Knight 2019년 10월 22일
Hi,
Yes the lookup table is somewhat larger,
I am not sure I understand your code though, I am running into errors when I try and execute it
Error in testing (line 4)
b=repelem('0',numel(txt));b(txt<95)='1';
dpb
dpb 2019년 10월 22일
편집: dpb 2019년 10월 22일
Worked just fine here...of course, txt is your input that I guess you called usertext, I didn't see any reason for more typing at the keyboard... :)
txt='TestINgOnE';
All it does is start w/ a string of '0' the length of the user input then substitute '1' for the logically T positions so have char() string to convert to numeric instead of logical array you had.
There is no error checking so will need to test for numel() == multiple of 5, etc., or the reshape() operation will fail at a minimum ...
James Knight
James Knight 2019년 10월 23일
Thanks , I am running it now.
I'm trying to understand exactly what it does though, how the code works?
Thankyou
James Knight
James Knight 2019년 10월 23일
Its the X and Y Intercepts I dont really undestand
dpb
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"

이 질문은 마감되었습니다.

태그

질문:

2019년 10월 22일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by