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

1 view (last 30 days)
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 Comments
Greg
Greg on 5 Dec 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.

Sign in to comment.

Answers (1)

ES
ES on 5 Dec 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

Categories

Find more on Variables 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!