My global variables are disappearing!

2 次查看(过去 30 天)
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
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
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 CenterFile Exchange 中查找有关 Title 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by