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 个评论
Adam
2017-12-4
doc guidata
would be a good place to start.
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
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!