Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

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

1 vue (au cours des 30 derniers jours)
Valeria Chacon
Valeria Chacon le 26 Oct 2016
Clôturé : MATLAB Answer Bot le 20 Août 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?

Réponses (2)

Image Analyst
Image Analyst le 27 Oct 2016
Try sscanf() or textscan() instead:
prompt = 'Enter some numbers: ';
userInput = input(prompt, 's')
numbers = cell2mat(textscan(userInput, '%f'))
  3 commentaires
Image Analyst
Image Analyst le 27 Oct 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 le 27 Oct 2016
Modifié(e) : Image Analyst le 27 Oct 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 le 27 Oct 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 commentaires
Valeria Chacon
Valeria Chacon le 27 Oct 2016
Modifié(e) : Valeria Chacon le 27 Oct 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 le 27 Oct 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.

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by