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

1 ビュー (過去 30 日間)
Akana Juliet
Akana Juliet 2021 年 6 月 22 日
コメント済み: Akana Juliet 2021 年 6 月 22 日
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.
  1 件のコメント
Akana Juliet
Akana Juliet 2021 年 6 月 22 日
Thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by