-1

I am starting with MatLab.

I created a guide UI. I went ahead and assumed I could just add new feild to the handles structure created.

Can I do this?

For example, when I refer to handle.s in my program, I get an error that I am reffering to a non existing feild.

handles.s was created to store a serial port object generated in a function that initialises serial communication between my PC and a microcontroller.

the serial port object has methods and feild of its own...could it be that I cannot pass an object as a feild to the handles object that contains the property of the guide UI?

Here is the code I am working with

    function [ s, flag] = setupSerial(comPort)
%Initialize serial port communication between Arduino and Matlab
%Ensure that the arduino is also communicating with Matlab at this time. 
%if setup is complete then the value of setup is returned as 1 else 0.

flag =1;
s = serial(comPort);
set(s,'DataBits',8);
set(s,'StopBits',1);
set(s,'BaudRate',9600);
set(s,'Parity','none');
fopen(s);
a='b';
while (a~='a')
    a=fread(s,1,'uchar');
end
if (a=='a')
    disp('serial read');
end
fprintf(s,'%c','a');
mbox = msgbox('Serial Communication setup.'); uiwait(mbox);
fscanf(s,'%u');
end

Within my UserInterface.m file, I pass over the object in a claaback function as follows

function SerialBtn_Callback(hObject, eventdata, handles)
% hObject    handle to SerialBtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA
comPort = get(handles.COMportTxt,'String');
if(~exist('serialFlag','var'))
    [handles.s, handles.serialFlag] = setupSerial(comPort);
end
end

I get the error when I press the 'Home' button. Here is the callback funciton

function HomeButton_Callback(hObject, eventdata, handles)
% hObject    handle to HomeButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
disp('home button pressed');
fprintf(handles.s,'%s', 'G2');
set(handles.CurrentPositionTxt, 'String', '0');
end

There error I get is the following

Reference to non-existent field 's'.

Here the GUI for you information

enter image description here

kamwo
  • 1,980
  • 1
  • 23
  • 32
Ethienne
  • 111
  • 1
  • 3
  • 12
  • 1
    Please post the code where you define the handles.s. Are you saving handles with guidata? – R. Schifini Aug 08 '15 at 19:13
  • I added the code. I don't think I am saving the handles with guidata no. simply passing over the object to a new feild that did exist before. – Ethienne Aug 09 '15 at 19:53

1 Answers1

1

The callback where handles.s is defined is not saving the handles object. You must save it using guidata in order to have it available later in another callback.

function SerialBtn_Callback(hObject, eventdata, handles)
% hObject    handle to SerialBtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA
comPort = get(handles.COMportTxt,'String');
if(~exist('serialFlag','var'))
    [handles.s, handles.serialFlag] = setupSerial(comPort);
end
guidata(hObject,handles)

Hope this helps.

R. Schifini
  • 9,085
  • 2
  • 26
  • 32