필터 지우기
필터 지우기

How to store variables in Matlab GUI

조회 수: 6 (최근 30일)
Aditya Tan
Aditya Tan 2019년 6월 25일
댓글: Oriol Brasco 2019년 6월 25일
Hi,
I am having trouble to store variables in Matlab GUI. I really couldn't grasp the concept of handles without an example.
So below is my GUI. What I'm trying to do is to (1) load my data based on the criteria from the two textboxes, (2) perform analyses and (3) plot the result by clicking the plot button. But for the sake of this question, let's simplify the problem into: (1) load Stock ID and Year by clicking Load Button and (2) plot the two points.
Now, how do I use guidata() to store variables yearid and sid so that I can call them on another function? Thanks a lot. Aditya
methods (Access = private)
% Button pushed function: LoadDataButton
function LoadDataButtonPushed(app, event)
% get the year ID from the YearEditField
yearid = app.YearEditField.Value;
% get the stock ID from the YearEditField
sid = app.StockIDEditField.Value;
end
% Button pushed function: DisplayPlotButton
function DisplayPlotButtonPushed(app, event)
plot(yearid, sid) % <--------- Problem???
end
end
Screen Shot 2019-06-25 at 18.25.03.jpg

채택된 답변

Oriol Brasco
Oriol Brasco 2019년 6월 25일
First add yearid and sid to the properties (Code Browser --> Properties), then these propierties form part of the app struct.
Once the properties are created, save the value you want to save when you push the loadData:
app.yearid = app.YearEditField.Value;
app.sid = app.StockIDEditField.Value;
In order to get the value just enter the struct like:
function DisplayPlotButtonPushed(app, event)
plot(app.yearid, app.sid)
end
You could also:
function DisplayPlotButtonPushed(app, event)
plot(app.YearEditField.Value, app.StockIDEditField.Value)
end
  댓글 수: 2
Aditya Tan
Aditya Tan 2019년 6월 25일
Hi Oriol,
Thanks for your answer.
But the following plotting code:
plot(app.UIAxes, app.yearid, app.sid, 'o', 'Markersize', 3);
Produces an error:
Error using plot
Not enough input arguments.
The following code, however,
plot(app.UIAxes, 10, 20, 'o', 'Markersize', 3);
works. May you help me again, please? Thanks. Aditya
Oriol Brasco
Oriol Brasco 2019년 6월 25일
app.YearEditField.Value
Is a char so try to convert it to double before plotting:
app.yearid=str2double(app.YearEditField.Value)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by