1

I want to create input dialog box in matlab. I am performing simple addition operation in MATLAB. Two variables name, a and b are needed to be given by user and then performing addition c=a+b; and display it in output. Both a and b should be positive integer only. I tried following:

    a = inputdlg({'Enter positive integer (a)'});     
    b = inputdlg({'Enter positive integer (b)'});        
    c=a+b;

But it is giving following error:

Undefined function or method 'plus' for input arguments of type 'cell'.

Please suggest how can i code the above program in described way.

1 Answer 1

1

That's because the output of inputdlg is a cell array containing a string; here a 1-cell array.

Hence you need to access the content of the cell array to perform the operation; for example using {curly brackets} : {a} and {b}.

In your case, since you are asking the use for a number, you need to convert the output, which is a string, to an actual number Matlab can use using for instance str2double, which operates on cell arrays

c = str2double(a) + str2double(b)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.