How to use guidata ?
4 views (last 30 days)
Show older comments
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
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!)
Accepted Answer
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
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;
More Answers (1)
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.
0 Comments
See Also
Categories
Find more on Dialog Boxes in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!