re entering a user inputted variable
Mostra commenti meno recenti
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 Commenti
Matt Fig
il 27 Ott 2012
Why don't you tell us what error you get?? When I copy/paste your code I get no error.
Walter Roberson
il 27 Ott 2012
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
il 27 Ott 2012
Mark Grano
il 27 Ott 2012
Walter Roberson
il 27 Ott 2012
In the case where you re-input, you do not recalculate nbrs or nbrx, but the "clear" has removed their original values.
Mark Grano
il 27 Ott 2012
Walter Roberson
il 27 Ott 2012
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".
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!