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)
Afficher commentaires plus anciens
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?
0 commentaires
Réponses (2)
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
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
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
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
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.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!