Don't use Global... Okay how do I switch to guidata??

1 vue (au cours des 30 derniers jours)
Kristin Hoeben
Kristin Hoeben le 4 Déc 2017
Réponse apportée : ES le 5 Déc 2017
I've read everywhere that using global is bad. But it was how I was taught. I'm trying to switch to using guidata. I included simple code of how I have used global variables before. Can someone tell me how I would revamp to use guidata? It's been difficult to find a visual example and I think it would help to compare what it's like before and after.
function OBIGGSSGUI
clear all; clc; close all;
disp(datestr(now))
%Define Global Variable....BAD???
global OGUIfig
OGUIfig.figure = figure('Visible','on','Name','OBIGGS Simulation'...
,'Units','Pixels','Position',[200,100,1000,600],'Color',...
[0.2 0.9 0.6],'NumberTitle','off','menubar','none');
fm = uimenu(OGUIfig.figure,'Label','Exit');
uimenu(fm,'Label','Exit','callback',{@exitfigure});
end
function exitfigure(varargin)
global OGUIfig
close(OGUIfig.figure)
end
  3 commentaires
Adam
Adam le 4 Déc 2017
Greg
Greg le 5 Déc 2017
Just a word of caution, I really wouldn't get too deep into guidata if you haven't already. It won't be long before AppDesigner is definitely the better choice. Then you'd just have to learn one more new way to share data.
But, if you must learn guidata, use guide to create a simple interface and see how guidata is used in the auto-generated code.

Connectez-vous pour commenter.

Réponses (1)

ES
ES le 5 Déc 2017
Normally, You can use handles structure and update your global variables in this structure.
handles.my_var = 5; % my_var would have been a global variable in your current implmentation
guidata(hObject, handles);% Update handles structure so that my_var can be accessed in all functions that have handles

Catégories

En savoir plus sur Variables 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!

Translated by