How to display data on uitable from selected name in the listbox GUI MATLAB?

6 次查看(过去 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 个评论
Aqilah Ahmad
Aqilah Ahmad 2022-6-6
Hi Mr Voss, the global variable p used for storing the table data. Since you said i don’t need it, I will delete it.
Aqilah Ahmad
Aqilah Ahmad 2022-6-7
Hi Mr. Jon, the error messages that i got is "Dimensions of matrices being concatenated are not consistent."
But I have change my coding and still got error. Here is my new coding:
i = 1:100;
%Indices=getappdata(handles.uitable1,'CurrentCell');
%Data=get(handles.uitable1,'Data');
Bulan = get(handles.edit2, 'String');
Jumlah = get(handles.edit3, 'String');
Ic = get(handles.edit6, 'String');
Akaun = get(handles.edit7, 'String');
Kod = get(handles.edit8, 'String');
Bank = get(handles.edit9, 'String');
Name = get(handles.listbox1, 'String');
NewName = Name{get(handles.listbox1,'value')};
%Data{Indices(1),Indices(2)} = NewName;
P(i,:)=[i Bulan NewName Ic Akaun Kod Bank Jumlah];
set(handles.uitable1, 'Data', P)
and the error said "Subscripted assignment dimension mismatch."
P(i,:)=[i Bulan NewName Ic Akaun Kod Bank Jumlah];

请先登录,再进行评论。

采纳的回答

Voss
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 CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by