How to display data on uitable from selected name in the listbox GUI MATLAB?
7 次查看(过去 30 天)
显示 更早的评论
Hi my dearest friends, I have a problem regarding GUI MATLAB.
I want to display data from a selected name in the listbox into uitable. Firstly, I would like to select a name from listbox then the data will be appeared in edit text boxes (No IC/PASSPORT, No AKAUN, KOD BANK, BANK). Next, when i push a button namely 'Add Row', the data from the edit text boxes that are came from selected name in the listbox will appeared in the uitable. However, my coding is not working.
Here is my coding:
function listbox1_Callback(hObject, eventdata, handles)
val = get(handles.listbox1, 'Value');
if ~isscalar(val)
set([handles.edit6 handles.edit7 handles.edit8 handles.edit9],'String','');
return
end
str = get(handles.listbox1, 'String');
if ischar(str)
% if listbox1 String is a character array, make it a scalar cell array
str = {str};
end
if strcmp(str{val},'MOHD RUSDI')
set(handles.edit6,'String', '780xxxxxxxx');
set(handles.edit7,'String', '7044xxxxxx');
set(handles.edit8,'String', '3x');
set(handles.edit9,'String', 'CIMx');
elseif strcmp(str{val},'WAN YUSOF')
set(handles.edit6,'String', '78xxxxxxxxxx');
set(handles.edit7,'String', '147xxxxxxxxxx');
set(handles.edit8,'String', '3x');
set(handles.edit9,'String', 'CIMx');
elseif strcmp(str{val},'MD AZHAR')
set(handles.edit6,'String', '720xxxxxxxxxxxx');
set(handles.edit7,'String', '10xxxxxxxxxxxxxx');
set(handles.edit8,'String', '2x');
set(handles.edit9,'String', 'MBx');
else
set([handles.edit6 handles.edit7 handles.edit8 handles.edit9],'String','');
end
function pushbutton1_Callback(hObject, eventdata, handles)
global p
NamaData = get(handles.listbox1,'String');
BlnData = get(handles.edit2,'String');
IcData = get(handles.edit6,'String');
AkaunData = get(handles.edit7,'String');
KodData = get(handles.edit8,'String');
BankData = get(handles.edit9,'String');
JumlahData = get(handles.edit3,'String');
p = [p; [{BlnData} {IcData} {AkaunData} {KodData} {BankData} {JumlahData}]];
set(handles.uitable1,'Data', p);
here is my GUI:

Thank you so much for your willingness to spend your time on helping me.
4 个评论
采纳的回答
Voss
2022-6-7
编辑:Voss
2022-6-7
If the table should initially be empty, then in the OpeningFcn, initialize the Data of handles.uitable1 to be cell(0,7). (That is, it should have 0 rows and the correct number of columns, which looks like 7.)
set(handles.uitable1,'Data',cell(0,7));
Then for the pushbutton Callback, try this:
function pushbutton1_Callback(hObject, eventdata, handles)
val = get(handles.listbox1,'Value');
if ~isscalar(val)
return
end
str = get(handles.listbox1,'String'); % str is all the names
NamaData = str{val}; % NamaData is the selected name only
BlnData = get(handles.edit2,'String');
IcData = get(handles.edit6,'String');
AkaunData = get(handles.edit7,'String');
KodData = get(handles.edit8,'String');
BankData = get(handles.edit9,'String');
JumlahData = get(handles.edit3,'String');
old_table_data = get(handles.uitable1,'Data');
new_table_data = [ ...
old_table_data; ... % existing table data
{BlnData IcData NamaData AkaunData KodData BankData JumlahData} ... % a new row of data
];
set(handles.uitable1,'Data',new_table_data);
% that's my best guess for the order of those things
% (BlnData, IcData, NamaData, etc.) based on the screen shot
But it looks like there's another edit box whose contents don't go to the table. Maybe use that instead of the selected item in the listbox? I'm not sure what's intended.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!