How to use guidata ?

3 views (last 30 days)
adi kul
adi kul on 28 Apr 2015
Edited: adi kul on 28 Apr 2015
Hello, I have 2 push buttons. One to take input from a file. And other is a button named "calculate".
Now under 2n'd push button I am taking 4 inputs from user as numbers. An now by calculating data from file uploaded by user and values given by user the "calculate" will trigger the calculations.
Now what error I am getting is, the values from 1st pushbutton are not getting saved under workspace to use by 2nd button. I tried guidata but could not make it work.
Can any one give me the solution to have guidata ?
Here is my code for 1st pushbutton:
[filename,pathname] = uigetfile('*.txt')
loaddata = fullfile(pathname,filename)
xy = load(loaddata);
a = xy(:,1)
b = xy(:,2)
c = xy(:,3)
d = xy(:,4)
handles.input1 = a;
handles.input2 = b;
handles.input3 = c;
handles.input4 = d;
guidata(hObject,handles)
  2 Comments
Adam
Adam on 28 Apr 2015
What is the code in your second pushbutton and what exactly is the error message? That code on its own looks fine (apart from the variable naming!)
adi kul
adi kul on 28 Apr 2015
Edited: adi kul on 28 Apr 2015
Okay. I can't put my code directly but I will give you an Idea what I have under 2nd push button.
I have these:
aa = get(handles.edit1,'String');
bb = get(handles.edit2,'String');
cc = get(handles.edit3,'String');
Which Takes input from user in variables. Now I have formula
y=b*bb;
x=a*aa;
So I am getting stuck at every formula under 2nd push button with error "Undefined function or variable b".

Sign in to comment.

Accepted Answer

Adam
Adam on 28 Apr 2015
Is y supposed to contain something from earlier? If not just pre-declare it to be the correct size in your pushbutton callback.
Your example code does not make use of anything from your first pushbutton though so it is hard to see where that fits into the problem.
If you are using something from the first pushbutton callback you must refer to it in the 2nd callback as e.g.
handles.input1
  4 Comments
Ilham Hardy
Ilham Hardy on 28 Apr 2015
inside the second pushbutton, you need to re-declare the variable a,b and c.
e.g.
a = handles.input1;
b = handles.input2;
c = handles.input3;
adi kul
adi kul on 28 Apr 2015
Thanks Adams and Ilham. That re-declaring solved my issue!

Sign in to comment.

More Answers (1)

Greig
Greig on 28 Apr 2015
As Adam said you need to get the input variables from the GUI handles. As soon as the first pushbutton callback is complete, a, b, c, and d don't exist anymore.
You want something like this in the callback for pushbutton2
a = handles.input1;
b = handles.input2;
You should also note that as you have written it, aa, bb, and cc are strings, so you calculations will all be wrong (try 2*'1' in the command line).You need to convert them to numbers...
aa = str2double(get(handles.edit1,'String'));
bb = str2double(get(handles.edit2,'String'));
cc = str2double(get(handles.edit3,'String'));
Also, if you are writing a serious GUI you should take time to make sure the the user inputs are correct. If the user types in a letter, str2double() will return NaN. It is worthwhile adding default values that replace any silly user input.

Categories

Find more on Dialog Boxes in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!