How to use guidata ?

7 visualizzazioni (ultimi 30 giorni)
adi kul
adi kul il 28 Apr 2015
Modificato: adi kul il 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 Commenti
Adam
Adam il 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 il 28 Apr 2015
Modificato: adi kul il 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".

Accedi per commentare.

Risposta accettata

Adam
Adam il 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 Commenti
Ilham Hardy
Ilham Hardy il 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 il 28 Apr 2015
Thanks Adams and Ilham. That re-declaring solved my issue!

Accedi per commentare.

Più risposte (1)

Greig
Greig il 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.

Categorie

Scopri di più su Entering Commands in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by