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

4 visualizzazioni (ultimi 30 giorni)
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');

Risposta accettata

DGM
DGM il 22 Giu 2021
Modificato: DGM il 22 Giu 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.

Più risposte (0)

Categorie

Scopri di più su Data Type Identification in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by