MATLAB Answers

How to use guidata ?

7 ビュー (過去 30 日間)
adi kul
adi kul 2015 年 4 月 28 日
編集済み: adi kul 2015 年 4 月 28 日
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 件のコメント
adi kul
adi kul 2015 年 4 月 28 日
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".

サインインしてコメントする。

採用された回答

Adam
Adam 2015 年 4 月 28 日
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 件のコメント
adi kul
adi kul 2015 年 4 月 28 日
Thanks Adams and Ilham. That re-declaring solved my issue!

サインインしてコメントする。

その他の回答 (1 件)

Greig
Greig 2015 年 4 月 28 日
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.

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by