How to use guidata ?
조회 수: 5 (최근 30일)
이전 댓글 표시
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
Adam
2015년 4월 28일
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!)
채택된 답변
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
Ilham Hardy
2015년 4월 28일
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;
추가 답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Dialog Boxes에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!