My global variables are disappearing!

6 ビュー (過去 30 日間)
John Callahan
John Callahan 2012 年 7 月 12 日
Apparently even my question are having errors. According to mathworks my question is less than four words long :(. I had to add this introduction so that it would work.
Hello again,
I posted a question ealier and have since managed to figure out the answer. The answer, however, just gave me an even stranger question.
First, the story. I have a GUI. It is a grumpy GUI. I declare a global variable called linkedObject at the initiation of the GUI. Later on I call this code:
function importButton_Callback(hObject, eventdata, handles)
global linkedObject;
fileName = get(handles.fileImpField, 'String');
delimStatus = get(handles.delimToggle, 'Value');
if delimStatus == 1
delim = get(handles.delimChar, 'String');
linkedObject.startDel(fileName, delim);
else
linkedObject.startRead(fileName);
end
set(handles.dispTable, 'Data', linkedObject.fullArray);
end
This code works fine. It gives me a shiny new array of imported data. It also pays attention to its global variable. When I have checked on the object at the end of the function it's properties are all as they should be (containing data).
The code that doesn't work is this:
function selColButton_Callback(hObject, eventdata, handles)
global linkedObject;
selectedColumn = linkedObject.getColumn(get(handles.selColField, 'Value'));
end
Apparently the list of columns suddenly no longer exists and my code tries to look in an empty variable for a cell array. After calling the first function these are the (important) listed properties.
Properties
selColumn: {1x18 cell}
fullArray: {1162277x18 cell}
After calling the second function (but before anything is actually done) these are the important listed properties.
Properties
selColumn: []
fullArray: []
In fact every property is simply [].
Also to note: The object's functions when used in the command window work just fine. It is when the gui gets involved that I have these issues.
Can anyone tell me whats going on? Any help is much appreciated :). Thank you.
Cheers,
John

採用された回答

Sebastian Holmqvist
Sebastian Holmqvist 2012 年 7 月 12 日
You could utilize e.g
setappdata(handles.gui_element, 'stored_var', data_var)
...
data_var = getappdata(handles.gui_element, 'stored_var')
  2 件のコメント
John Callahan
John Callahan 2012 年 7 月 12 日
Thank you. This is what I ended up going with. I will leave the question up though. That way if someone finds an answer then someone else may be able to benefit from it.
Sebastian Holmqvist
Sebastian Holmqvist 2012 年 7 月 17 日
Absolutely. But by accepting an answer you don't close the question. You merely state that the question is resolved and people can still find it.

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2012 年 7 月 12 日
編集済み: Jan 2012 年 7 月 12 日
The best idea is to avoid globals strictly. As you see, they are prone to errors and debugging the source of changes (or missing changes as in your case) is very complicated or near to impossible. So you run in exactly these problems global 's are famous for.
Therefore I dare not to post an analysis of your problem, but to suggest using a different approach.
  2 件のコメント
John Callahan
John Callahan 2012 年 7 月 12 日
編集済み: John Callahan 2012 年 7 月 12 日
Thank you for your insight. Is there another way to get all of my gui functions to reference the same object without passing it in as an argument?
Jan
Jan 2012 年 7 月 12 日
See: set(FigureHandle, 'UserData'), set/getappdata, guidata.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by