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

1 回表示 (過去 30 日間)
Kristin Hoeben
Kristin Hoeben 2017 年 12 月 4 日
回答済み: ES 2017 年 12 月 5 日
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
Adam 2017 年 12 月 4 日
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 Center および File ExchangeVariables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by