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

1 次查看(过去 30 天)
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 个评论
Greg
Greg 2017-12-5
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.

请先登录,再进行评论。

回答(1 个)

ES
ES 2017-12-5
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

类别

Help CenterFile Exchange 中查找有关 Variables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by