フィルターのクリア

Troubleshooting code for prompt and inputdlg

1 回表示 (過去 30 日間)
fiona rozario
fiona rozario 2017 年 6 月 10 日
コメント済み: fiona rozario 2017 年 6 月 10 日
The following code works fine for any user input for N:
N = input('Number of pieces to break into ');
rowgroups = diff( round(linspace(0,r,N+1)));
But when I try to use the following code, I get an error:
Undefined function 'plus' for input arguments of type 'cell'.
Error in sharebased_nshares (line 62) rowgroups = diff( round(linspace(0,r,N+1)));
prompt = {'Enter no. of shares:'};
dlg_title = 'Input';
num_lines = 1;
defaultans = {'2'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
N=answer;
rowgroups = diff( round(linspace(0,r,N+1)));
What is going wrong and where?

採用された回答

Star Strider
Star Strider 2017 年 6 月 10 日
You need to convert ‘answer’ to a number:
N=str2double(answer);
That should work with your code.
  4 件のコメント
Star Strider
Star Strider 2017 年 6 月 10 日
@fiona rozario — My pleasure!
fiona rozario
fiona rozario 2017 年 6 月 10 日
Thank you... I hadn't thought about this.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2017 年 6 月 10 日
N is a cell so use str2double(). See this robust snippet:
% Ask user for one integer number.
defaultValue = 2;
titleBar = 'Enter an integer value';
userPrompt = 'Enter the integer';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Round to nearest integer in case they entered a floating point number.
integerValue = round(str2double(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(integerValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nTry replacing the user.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by