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