MATLAB: Troubleshooting code for prompt and inputdlg

inputdlgprompt

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?

Best Answer

You need to convert ‘answer’ to a number:
N=str2double(answer);
That should work with your code.
Related Question