Help creating loop that converts text to numbers

Hi, this is a follow-up on a question I asked yesterday.
I have a loop that asks the user to input sentences until the word 'end' is typed.
After the loop has ended, I want it to display the sentences typed as inegers, using the "char" command
ex
char([101 102 103])
but reversed since it would be taking the letters and converting them to integers.
Here is what I have so far.
The other loop to execute this specific function is probably extremely wrong but I am an absolute beginner and was just trying to follow a similar format to the first loop I recieved some help with yesterday.
clc
N=input('what is N?: ') %establishes N
all_A = {};
while true
A = input('give me a sentence!: ','s'); %asks user for sentence input
if strcmpi(strtrim(A), 'end');
break;
end %will end the loop if end is typed
all_A{end+1} = A;
end
all_B={};
while false
B= input('give me a sentence!: ','s');
if strcmpi(strtrim(B));
num2str(char[B])
end
end

回答 (1 件)

dpb
dpb 2021 年 10 月 22 日

0 投票

>> A='Now is the time...';
>> format short
>> disp(double(A))
78 111 119 32 105 115 32 116 104 101 32 116 105 109 101 46 46 46
>> char(double(A))
ans =
'Now is the time...'
>>
The converse of char() is simply one of the numeric classes; by default everything numeric in MATLAB is double so I used it. Could just as well have used
>> int8(A)
ans =
1×18 int8 row vector
78 111 119 32 105 115 32 116 104 101 32 116 105 109 101 46 46 46
>>

4 件のコメント

Alex Skantz
Alex Skantz 2021 年 10 月 22 日
編集済み: dpb 2021 年 10 月 22 日
This works however I need to get all the sentences converted to integers before the word "end" is typed. This code only seems to work for the last sentence. Any way I can go about doing that?
I edited your text example so it looks like:
A=input('give me a sentence!: ','s');
format short
disp(double(A));
because it was only showing the numbers for the example sentence you used which was "Now is the time..."
I wanted it to look something like :
Give me N: 10
Give me a sentence: hello goodbye
Give me a sentence: goodbye hello
Give me a sentence: end
and then it would display:
104 101 108 108 111 103 111 111 100 98 121 101
103 111 111 100 98 121 101 104 101 108 108 111
dpb
dpb 2021 年 10 月 22 日
編集済み: dpb 2021 年 10 月 23 日
You'll need to save the input from the user as a cellstr array or string array as it is being entered and then loop over however many elements there are after the input loop is finished to get the input/output in that order.
NB: you definitely don't need the format statement everywhere; I just had it because for what I am doing locally, I'm using the bank format which would display two decimals that I didn't want to clutter up the post.
A=[];
while true
s = input('give me a sentence!: ','s'); %asks user for sentence input
if strcmpi(strtrim(s), 'end');
break;
end %will end the loop if end is typed
A=[A;string(s)];
end
for i=1:snumel(A)
disp(double(A{i})
end
may be close.
NB: AIR CODE! UNTESTED!
Alex Skantz
Alex Skantz 2021 年 10 月 23 日
so the code is almost perfect but instead of displaying numbers it just displays 'NaN'. I apologize for the inconvenience but as I am an absolute beginner I truly am unsure what can do to fix it. Any other suggestions?
dpb
dpb 2021 年 10 月 23 日
As noted, UNTESTED code...I had never tried double on a string; I presumed it would behave as expected; turns out it doesn't. A string variable is higher abstraction and contains a cellstr() underneath, but to convert to underlying numeric turns out you have to explicitly address that content -- "Use the curlies, Luke!"
disp(double(A{i})

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2021a

質問済み:

2021 年 10 月 22 日

編集済み:

dpb
2021 年 10 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by