Clear Filters
Clear Filters

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

6 views (last 30 days)
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 Comments
Aqilah Ahmad
Aqilah Ahmad on 6 Jun 2022
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 on 7 Jun 2022
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];

Sign in to comment.

Accepted Answer

Voss
Voss on 7 Jun 2022
Edited: Voss on 7 Jun 2022
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.
  2 Comments
Aqilah Ahmad
Aqilah Ahmad on 7 Jun 2022
Hi Mr. Voss, I would like to say thank you because my coding works perfectly fine as it should. Please have a nice day and may God bless you.
here is the result:

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!