Effacer les filtres
Effacer les filtres

How to display an array that is stored in a cell in my input statement?

2 vues (au cours des 30 derniers jours)
Akana Juliet
Akana Juliet le 22 Juin 2021
Commenté : Akana Juliet le 22 Juin 2021
Hi everyone, I was wondering how can I display an array that is stored in a cell in my input statement?
I have 1x4 cell of arrays called "badValues" (each Array includes numerical values)
I am trying to prompt the user with:
userSelectedValue = input('Please Input a number from 1-100 except for',(badValues{k}):'\n');
Obviously this code is very improper and doesn't work. I am trying to display the badValues based on what the user selects k as in a previous loop. I have four different situations, I wanted to pull the badValues based on which k is called (1-4).
Any help would be greatly appreciated!
userSelectedValue = input('Please Input a number from 1-100 except for',(badValues{k}):'\n');

Réponse acceptée

DGM
DGM le 22 Juin 2021
Modifié(e) : DGM le 22 Juin 2021
There are probably a bunch of ways to do this, but here's a simple way:
badValues = {[1 2 3 4],[1; 2; 3; 4],[1 2; 3 4]};
k = 1; % try different k to see that the output format is unchanged
bvstring = mat2str(reshape(badValues{k},1,[]));
prompt = sprintf('Please Input a number from 1-100 except for the following: %s\n',bvstring);
userSelectedValue = input(prompt);
I decided to just display a reshaped array (it's easier to read, and contextually, the shape of the array shouldn't matter for the prompt anyway). It's not a very conversational way of presenting the information, but it should suffice.
I would posit that most usage of input() is entirely unnecessary and a generally tedious and error-prone way to collect values/parameters. Then again, I know a lot of coursework guides students to use this type of design. Considering my opinion on the topic, you can probably see why I would be quick to assume sufficiency has been met.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by