Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

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

1 Ansicht (letzte 30 Tage)
Valeria Chacon
Valeria Chacon am 26 Okt. 2016
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
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?

Antworten (2)

Image Analyst
Image Analyst am 27 Okt. 2016
Try sscanf() or textscan() instead:
prompt = 'Enter some numbers: ';
userInput = input(prompt, 's')
numbers = cell2mat(textscan(userInput, '%f'))
  3 Kommentare
Image Analyst
Image Analyst am 27 Okt. 2016
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 am 27 Okt. 2016
Bearbeitet: Image Analyst am 27 Okt. 2016
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 am 27 Okt. 2016
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 Kommentare
Valeria Chacon
Valeria Chacon am 27 Okt. 2016
Bearbeitet: Valeria Chacon am 27 Okt. 2016
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 am 27 Okt. 2016
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.

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by