re entering a user inputted variable

hello, i just have one very very minor issue with another area in which i need to make sure the number is ten digits long after everything is taken out, and if it is not ten digits long, it needs to ask to enter stra again and re run the program.
i have clear;clc;
stra =input('Enter a 10 digit phone number: ','s');
num2str(stra());
nbrs = regexpi(stra, '\d');
nbrx = stra(nbrs);
num2str(nbrx());
cnt=length(nbrx);
if length(nbrx)<10
clear;clc; disp('Invalid input'); strb=input('Please enter a TEN digit phone number: ','s'); stra=strb;end
a=(nbrx(1));
although when i run this fragment i get an error. any thoughts on how to fix this? i imagine its simple. its just when i re enter stra it does not put it through the first part of the program that converts stra into what nbrx is. i get an error that nbrx is undefined.

댓글 수: 7

Matt Fig
Matt Fig 2012년 10월 27일
Why don't you tell us what error you get?? When I copy/paste your code I get no error.
Walter Roberson
Walter Roberson 2012년 10월 27일
That line num2str(nbrx()); is not doing anything useful.
What is that "clear" command doing inside the "if" ?
Why are you using stra() instead of just stra ?
Mark Grano
Mark Grano 2012년 10월 27일
Undefined function 'nbrx' for input arguments of type 'double'.
Error in TelephoneKeypad (line 29) a=(nbrx(1));
this is the error i get
as for the num2str(nbrx()); and the clear, were just different thing i tried to fix it with. I thought that maybe it wasn't making nbrx a string, and i thought that maybe it wasn't clearing nbrx when i re entered the variable. it was a shot in the dark though, i didn't see why it wouldn't clear it. and my professor actually told us to use that. i figured because it was an array. this is only a fragment of the code however, and line 29 is: a=(nbrx(1));
Mark Grano
Mark Grano 2012년 10월 27일
also the error is when length(nbrx) < 10 it works fine when the length is 10. just to clarify if there was confusion there
Thanks for both your help
Walter Roberson
Walter Roberson 2012년 10월 27일
In the case where you re-input, you do not recalculate nbrs or nbrx, but the "clear" has removed their original values.
Mark Grano
Mark Grano 2012년 10월 27일
why won't it re-calculate it? shouldn't it re input stra back into all the previous functions with the new value?
No. When you have an expression such as
nbrx = stra(nbrs);
then nbrx would be calculated with the values of stra and nbrs that were active right at the time the statement executed, and no "history" of having been calculated from stra and nbrs would be recorded and "replayed" to recalculate nbrx when new values were assigned to any of the variables. If you want to recalculate nbrx then you need to have another executable statement that calculates it.
And besides, remember that you "clear" in the meantime: if history of how nbrx had been kept somehow, you would have asked to have erased it by executing the "clear".

댓글을 달려면 로그인하십시오.

 채택된 답변

Image Analyst
Image Analyst 2012년 10월 27일

0 개 추천

Here's one way to do it:
defaultValue = '(123)456 - 7890';
titleBar = 'Enter the number';
userPrompt = 'Enter the 10 digit phone number';
numberCount = 0;
while numberCount < 10
% Ask user for a number.
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
strInput = char(caUserInput)
numberCount = 0;
theNumber = [];
[a1 a2 a3 a4 a5 a6 indexes] = regexpi(strInput, '\d')
numberCount = length(a4)
onlyTheNumbers = char(a4)
fprintf('The numbers = %s\n', onlyTheNumbers);
end
msgbox('Success!');

댓글 수: 2

Mark Grano
Mark Grano 2012년 10월 27일
i like your style with the menu. i want to learn how to use those in my projects. but this works great except for i don't want it to calculate anything when the user enters less than ten digits. it needs to say that it was invalid and ask the user to re enter the number. the rest of the program actually produces the keytones that sound when you actually dial them on a phone. just so you don't think thats the whole program haha. thanks again
Mark Grano
Mark Grano 2012년 10월 27일
nevermind, i just realized that it works perfect! haha, i thought it was still running the numbers through the program even if there wasn't enough, but it wasn't.
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by