Global arrays using GUIDE

1 view (last 30 days)
Jay
Jay on 3 May 2016
Edited: VBBV on 4 Jul 2018
I have an array (1,3) of values in the edit box 4 callback function (HMS1).
Similarly I have another array in the edit box 6 callback function (again a (1,3) array labelled HMS2).
Now at the end of the edit box 6 callback function I have the commands
LST = zeros(1,3) % Initialize the array
LST(1,1) = HMS1(1,1) + HMS2(1,1)
During runtime I have the error 'Undefined function or variable 'HMS1'.'
LST is initialised (with zeros) but not populated (values not added and populated).
The only reason I can perceive is that this is due to the edit box 4 callback functions array is not being referenced.
Is this correct?
If so how do I declare an array value to be a global type?
I have tried to initialize HMS1 and HMS2 in the opening function and still have the same issue.
  3 Comments
Jay
Jay on 4 May 2016
Edited: Jay on 4 May 2016
Thanks Stephen.
I appreciate that you directed me to the required reading rather than just giving me the answer.
After reviewing the information you attached I can not see how to obtain values from another function that is not used in a figure.
The values I require to be accessed are not outputted to a figure.
All relate to obtaining values from a figure. I have also tried to find where arrays (or even single values) are referenced between functions and could not find anything useful.
Adam
Adam on 4 May 2016
Edited: Adam on 4 May 2016
Your whole GUIDE file, containing the two callbacks you mention in your question, is the figure. The handles structure that gets passed to every callback as discussed in the above link as one method for sharing data amongst callbacks is the one I generally use.
guidata allows you to set and retrieve data stored in the handles structure.
You have to use it with care, never overwrite any of the existing data on handles (the GUI objects I mean, not something you add yourself) and never ever pass anything other than the full handles struct to the guidata function.
Other than that the handles structure is just a christmas tree, you can hang whatever you want on it, use guidata to save it back into the main 'figure' and then in your next callback the 'handles' argument you receive will include the data you previously attached to it.

Sign in to comment.

Accepted Answer

Jan
Jan on 4 May 2016
And an explicit implementation of the methods explained in the links Stephen has posted:
function editbox4callback(hObject, EventData)
handles = guidata(hObject);
HMS1 = rand(1, 3);
handles.HMS1 = HMS1;
guidata(hObject, handles);
end
function editbox6callback(hObject, EventData)
handles = guidata(hObject);
HMS2 = rand(1, 3);
HMS1 = handles.HMS1;
disp(HMS1 + HMS2);
end
  3 Comments
VBBV
VBBV on 4 Jul 2018
Edited: VBBV on 4 Jul 2018
Sorry to interrupt in this thread. But I have similar problem with array handling in GUI. when I implement the code shown by Jan, I get an error "Reference to non existent field, A = handles.A even though I have that data array present in callback ...where A is data array. I get this error in the CellEditcallback
Jan
Jan on 4 Jul 2018
Edited: Jan on 4 Jul 2018
@Vasishta Bhargava: Please open a new thread and post your code and a copy of the complete error message there.

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!