0

i have database named 'data' , in gui i have 3 editbox which are name,gpa,salary and a push button . have data in database like this :

field= {'name','gpa','salary'}
newdata = {'Andre',3.2,$1000}

my program like this :

conn = database('db1_mysql','root','');
sql = ['select * from data where name =','''',handles.name,''''];
data = fetch(conn,sql)
handles.data = data;
guidata(hObject,handles)
if isempty(data)
  msgbox('data not found')
  set(handles.gpa,'String','')
  set(handles.salary,'String','')
else
  set(handles.gpa,'String',data(1,2))
  set(handles.salary,'String',data(1,3))
end

what i want is if i type Andre in name editbox in gui , his gpa and salary show up in others edit box . but i'm getting error : Index exceeds matrix dimensions. my database have 1 row , and 3 coloumn . how can it not working. Im using matlab R2016a

1 Answers1

0

The result of fetch in your case is going to be a struct, therefore to access the individual elements you need to use dot referencing instead of () indexing.

set(handles.gpa, 'String', data.gpa)
set(handles.salary, 'String', data.salary)

If you want to show salary as a whole number you can convert it using num2str

set(handles.salary, 'String', num2str(data.salary, '%d'))
Suever
  • 64,497
  • 14
  • 82
  • 101
  • Thank you, it work very well !!!. 1 more thing sir, if salary show like this '1e+06' how to fix that ? thanks a lot before – Alfi Nugraha Sep 14 '16 at 02:24
  • @AlfiNugraha Updated – Suever Sep 14 '16 at 03:03
  • sir, would you like to help me solving this problem please ? http://stackoverflow.com/questions/39488338/reference-to-non-existent-field-error-when-showing-database-data-to-editbox-in-g . Thanks b4 – Alfi Nugraha Sep 14 '16 at 12:26