Global arrays using GUIDE
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Adam
2016 年 5 月 4 日
編集済み: Adam
2016 年 5 月 4 日
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.
採用された回答
Jan
2016 年 5 月 4 日
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 件のコメント
VBBV
2018 年 7 月 4 日
編集済み: VBBV
2018 年 7 月 4 日
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
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!