I was designing a simple program(script file) for addition, a+b=c

3 views (last 30 days)
I am new in MATLAB, I was designing a simple program(script file) for addition, a+b=c. where a is variable of 1x10, b is 1x10,and c is will also 1x10. but now if i need to change values of a and b from workspace, how to change it?. I am changing those values values of 'a' from workspace and runnning program, but these values are not changing. so my program is just fixed. i cant solve for any other value of 'a' and 'b'. please help me.
  1 Comment
Mohammad Sami
Mohammad Sami on 24 Jan 2020
You should make a function.
function c = add(a,b)
c = a + b;
end
You can then call your function to add any values, as long as the their sizes are same.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 24 Jan 2020
If you want to do it from the workspace, set a break point just before you are going to use a and b. Then double-click on the variable name in the workspace panel to bring up the variable editor. Then type in the values you want, and click the continue >> button on the tool ribbon.
Most people would not do it that way - not sure why you want to.
  2 Comments
pawan singh
pawan singh on 24 Jan 2020
Edited: Image Analyst on 24 Jan 2020
Sir, actually I would like to make program to add two coloumns and get third coloums. same as like in excel.
in excel, if we are changing values of our two input coloums its automatically changes values in third obtained coloumn. in similar fashion, i want to make a program.
but here in MATLAB, I am not able to change any input variable values.
function c = add(a,b)
c = a + b;
end
after running this code
function c = add(a,b)
|
Error: Function definitions are not permitted in this context
this error is coming out.
Image Analyst
Image Analyst on 24 Jan 2020
What I would do is to make a GUI in GUIDE or App Designer, and place a spreadsheet (uitable) on it. Then have a button that says Add on it. The user then runs the program and can type anything into columns 1 and 2 and then push the Add button. Then in the Add button callback have this code
data = handles.uitable1.Data;
a = data(:, 1); % a is column 1.
b = data(:, 2); % b is column 2
c = add(a, b); % Call your function, or, more simply, just say c = a + b;
data(:, 3) = c; % Stuff c into column 3
handles.uitable1.Data = data; % Put data back onto GUI

Sign in to comment.

More Answers (0)

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!