Unable to modify GUIDE variables

So I am trying to continuously read data from an arduino-based instrument and plot it using MATLAB, but I'm getting hung up on something that seems really basic. I declare a matrix A in my OpeningFcn (not showing comments and code relevant to serial initialization, which all works just fine):
function tutorial_OpeningFcn(hObject, eventdata, handles, varargin)
handles.A = [0 1];
handles.arduino_port.BytesAvailableFcn = {@dataReceivedCallback, handles};
guidata(hObject, handles);
end
and I have a callback function for when I receive more than 50 bytes of serial data (which I have confirmed is working beautifully)
function dataReceivedCallback(hObject, eventdata, handles)
temp = fread(hObject, 50)';
handles.A = [handles.A temp];
end
but for whatever reason it throws an error when I try to assign handles.A, which I have posted below.
Not enough input arguments.
Error in tutorial>dataReceivedCallback (line 98)
handles.A = [handles.A temp];
Error in instrcb (line 42)
feval(val{1}, obj, eventStruct, val{2:end});
Warning: The BytesAvailableFcn is being disabled. To enable the callback property
either connect to the hardware with FOPEN or set the BytesAvailableFcn property.
This seems like a trivial issue, but I can't make heads or tails of it. The same thing happens when I try to increment a simple dummy variable. I'm new to MATLAB and come from a C/C++ background and I'm treating 'handles' like a global class and 'A' like a value variable in that class. I don't see why this doesn't work. The same exact code works when I run it with vanilla variables in the command window.

回答 (1 件)

Swathik Kurella Janardhan
Swathik Kurella Janardhan 2017 年 4 月 20 日

0 投票

To share data between the callbacks, you can use 'UserData' property of 'hObject' as below inside the callback function:
temp = fread(hObject, 50)';
data = get(hObject,'UserData');
data = [data temp];
set(hObject,'UserData',data);
In the opening function, you can initialize 'UserData' as below:
data = [0 1];
set(hObject,'UserData',data);
You can also refer to the GUIDE coding app behavior for different ways to share data among callbacks.

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

タグ

質問済み:

2017 年 4 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by