How to store variables in Matlab GUI
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Aditya Tan
le 25 Juin 2019
Commenté : Oriol Brasco
le 25 Juin 2019
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
0 commentaires
Réponse acceptée
Oriol Brasco
le 25 Juin 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 commentaires
Oriol Brasco
le 25 Juin 2019
app.YearEditField.Value
Is a char so try to convert it to double before plotting:
app.yearid=str2double(app.YearEditField.Value)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Graphics Object Programming dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!