Is it possible to indicate a number with "any" units, tens, or hundreds?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Francesco Maria
il 13 Feb 2023
Commentato: Francesco Maria
il 15 Feb 2023
Hi, i'm new to MATLAB and i'm really just into the basics. I wanted to make a check of a time inserted by an input, so the program can the reiterate the input if the time inserted is wrong or non-existent (in this case time is an integer in the format "hhmm"). The check i've made is long and obsolete and i'd like to shorten it. Is it possible to use a number indicating any units? For example: i want to exclude all numbers between 540 and 549 but i don't want to manually say ">=540 && <550" for all the timing i want to exclude. Thanks a lot for your help
1 Commento
Rik
il 13 Feb 2023
Why is your proposed solution not acceptable? Can you give examples of the kind of input data you want to parse and what results you expect?
Risposta accettata
John D'Errico
il 13 Feb 2023
Modificato: John D'Errico
il 13 Feb 2023
There is not really any trivial way to test for what you want, than to just test for a number in that range. And that will require the two tests that you already know how to perform.
Could you extract the units digit, or perhaps the hundreths digit? Well, yes. mod can help. Thus if a number is an integer, then the units digit is just mod(x,10).
And, I suppose the 10's digit of an integer x can be gained by mod(floor(x/10),10). Similarly, the higher order digits can be gained in a similar way.
Is that really any simpler than what you were doing? For example this one test:
mod(floor(x.100),10) == 0
compared to two simpler tests to write? Don't forget that the complexity of code is important too. Can you read what you wrote, next year when you need to debug your code?
Your choice, I guess.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!