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

5 次查看(过去 30 天)
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');

采纳的回答

DGM
DGM 2021-6-22
编辑:DGM 2021-6-22
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by