How to store variables in Matlab GUI

14 views (last 30 days)
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

Accepted Answer

Oriol Brasco
Oriol Brasco on 25 Jun 2019
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 Comments
Aditya Tan
Aditya Tan on 25 Jun 2019
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 on 25 Jun 2019
app.YearEditField.Value
Is a char so try to convert it to double before plotting:
app.yearid=str2double(app.YearEditField.Value)

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming 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!