MATLAB Answers

0

App Designer: populate list box with string from excel file then compare user input with excel to populate "next" list box?

Dear MATLAB users,
I am attempting to create an app with App Designer (2019a) which does the following:
  1. Loads an Excel file (text/string data only) with user file selection
  2. Populates ListBox1 with first row of Excel file
  3. Uses the value selected by the user from ListBox1 to populate ListBox2 with options listed "below"/in rows below option selection (from Excel sheet).
  4. This selection continues until all options from the first row have been used.
A simplified example of my Excel sheet is as follows:
sample_excel.JPG
I have successfully loaded the excel sheet and populated ListBox1 (albeit I believe inefficiently). But cannot get "strcmp" to return a "1" for the row where the value selected appears in the Excel sheet. Furthermore, if I try to make my Excel data when converted to a cell array ("O") as a global variable ("app.O") the population of my first ListBox does not work. My call backs for the "LoadExcelTemplate" pushbutton and ListBox1 are as follows:
% Button pushed function: LoadExcelTemplateButton
function getexcelfile(app, event)
[file,path]=uigetfile('../*.xlsx');
app.OPS=readtable(file,'ReadVariableNames',0,'TextType','string','TreatAsEmpty',{''})
numOPS=width(app.OPS)
set(app.ListBox1,'Enable','on')
for i=1:numOPS
O{i}=app.OPS(:,i);
app.ListBox1.Items(i)=cellstr(O{i}{1,1})
end
end
% Value changed function: ListBox1
function list1items(app, event)
OP1 = app.ListBox1.Value;
numOPS=width(app.OPS);
for i=1:numOPS
O{i}=app.OPS(1,i);
end
x=strcmp(O,OP1);
end

  3 Comments

Did you check the value of O and OP1 when evaluating the strcmp with the debugger? If so, did it fail even if both had the same value?
Thanks for the comment, Jesus! I finally figured it out -- the correction:
% Value changed function: ListBox1
function list1items(app, event)
app.OP{1,1} = app.ListBox1.Value;
for i=1:app.numOPS
O{i}=app.OPS(:,i);
S(i)=cellstr(O{i}{1,1});
end
app.x(1,1)=find(strcmp(S,app.OP{1,1}))
numh=height(O{app.x(1,1)})-1;
for j=1:numh;
app.ListBox2.Items(j)=cellstr(O{app.x(1,1)}{j+1,1});
end
end
I am happy for you! If you would, please post your solution as an answer and accept it when possible (I think its a delay of 24 hours for self-answers). This way, if someone has the same problem one day they will have a possible solution avaliable :)

Sign in to comment.

1 Answer

Answer by Holly Huellemeier on 9 Dec 2019
 Accepted Answer

I figured it out:
% Button pushed function: LoadButton
function getexcelfile(app, event)
[file,path]=uigetfile('../*.xlsx');
app.OPS=readtable(file,'ReadVariableNames',0,'TextType','string','TreatAsEmpty',{''})
app.numOPS=width(app.OPS)
set(app.ListBox1,'Enable','on','Visible','on')
set(app.Label,'Visible','on')
for i=1:app.numOPS
O{i}=app.OPS(:,i);
app.ListBox1.Items(i)=cellstr(O{i}{1,1})
end
end
% Value changed function: ListBox1
function list1items(app, event)
app.OP{1,1} = app.ListBox1.Value;
for i=1:app.numOPS
O{i}=app.OPS(:,i);
S(i)=cellstr(O{i}{1,1});
end
app.x(1,1)=find(strcmp(S,app.OP{1,1}))
numh=height(O{app.x(1,1)})-1;
for j=1:numh;
app.ListBox2.Items(j)=cellstr(O{app.x(1,1)}{j+1,1});
end
end

  0 Comments

Sign in to comment.