run loop once with a click on push button
Show older comments
I'm writing a Lucky Draw Code. Name and number of people from excel file. I want to get : when I Click, Run loop once and show a name of lucky person. But now when I click, run loop (start to end ) and showing all names. See the code below and please show me the way how to overcome this problem. Thanks in advance.
clc
T = readtable('Name List.xlsx');
Count = height(T); % Count the number of people
A=1:Count;
for i = 1:Count
handles.counter = handles.counter + 1; % Count the click
nextValue = handles.counter
if i == nextValue
randId(i) = randperm(length(A),1); % generate the one random number
R = A(randId(i)) % initialize R to be the one number of A
Lucky_Name = T(R,:) % Name of person from excel list
A(:,randId(i)) = [] % remove those one lucky number from A
% Show the name of lucky person in UI
B=table2cell(Lucky_Name);
set(handles.uitable1, 'ColumnWidth', {50,100,100,150,150});
set(handles.uitable1, 'ColumnName', {'Sr No.', 'ID', 'POSITION', 'NAME', 'DEPARTMENT'});
set(handles.uitable1,'FontWeight','bold')
set(handles.uitable1,'BackgroundColor',[1 1 0],'ForegroundColor',[0 0.4470 0.7410]);
set(handles.uitable1,'Data',B);
else
disp('');
end
end
guidata(hObject, handles);
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!