필터 지우기
필터 지우기

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

조회 수: 1 (최근 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