フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

after removing characters out of my string, how can I put the numbers in a matrix

1 回表示 (過去 30 日間)
Valeria Chacon
Valeria Chacon 2016 年 10 月 26 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
prompt = 'Enter atleast 10 numbers: ';
str='0';
while length(regexp(str,'\d'))~=10
str = input(prompt,'s');
end
N=length(str);
count=1;
for k=1:N
v=str2double(str(k));
if isnan(v)==0
str(count)=v;
count=count+1;
disp(v);
end
end
this is currently the code I have right now and it's working except for the fact that when it spits out the numbers v ends up only holding the value of the last number it spit out. How can I put together all the numbers it spit out and put them into a matrix?

回答 (2 件)

Image Analyst
Image Analyst 2016 年 10 月 27 日
Try sscanf() or textscan() instead:
prompt = 'Enter some numbers: ';
userInput = input(prompt, 's')
numbers = cell2mat(textscan(userInput, '%f'))
  3 件のコメント
Image Analyst
Image Analyst 2016 年 10 月 27 日
That is not a question about my code. I don't have a variable called v. With my code, it does spit the variables (numbers) out to the command line because I did not put a semicolon at the end of the line. Is there any particular reason why you want to do it your much more complicated way than the simple way I showed you?
Image Analyst
Image Analyst 2016 年 10 月 27 日
編集済み: Image Analyst 2016 年 10 月 27 日
Try this to see if the user entered exactly 10 numbers:
numNumbers = length(numbers);
if numNumbers ~= 10
message = sprintf('You entered %d numbers instead of 10 numbers. Try again', numNumbers);
uiwait(errordlg(message));
end

Star Strider
Star Strider 2016 年 10 月 27 日
I always use the inputdlg function rather than input. I had problems with your input code, and abandoned it when it threatened to crash MATLAB. That seems to be the problem.
This works:
prompt = 'Enter at least 10 numbers: ';
nstrc = inputdlg(prompt);
nstr = nstrc{:};
N=length(nstr)
count=1;
for k=1:N
v=str2double(nstr(k));
if isnan(v)==0
str(count)=v;
count=count+1;
disp(v);
end
end
It still only understands ‘number’ as ‘single-digit integer’. If you want it to recognise numbers greater than 9, I’ll leave that for you to sort.
  2 件のコメント
Valeria Chacon
Valeria Chacon 2016 年 10 月 27 日
編集済み: Valeria Chacon 2016 年 10 月 27 日
Thank you! Is there any way that I can incorporate a function to make sure that no matter how many characters they input, there has to be 10 numbers? I tried my code but it gave me an error
Star Strider
Star Strider 2016 年 10 月 27 日
My pleasure!
This will limit the input to at most 10 numbers, and it also now likes numbers of more than one digit. You can loop back if there are fewer than 10 to ask for more. I leave that to you. (You can use the length of ‘nstrsplit’ to find out how many numbers were entered.)
The (slightly revised) Code:
prompt = 'Enter at least 10 numbers, separated by a comma: ';
nstrc = inputdlg(prompt)
nstrsplit = regexp(nstrc{:}, ',', 'split')
nstr = nstrsplit(1:10)
N=length(nstr)
count=1;
for k=1:N
v=str2double(nstr{k});
if isnan(v)==0
str(count)=v;
count=count+1;
disp(v);
end
end
I use cell arrays extensively here. If you are not familiar with them, see the documentation on Cell Arrays for information on addressing and using them.

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by