run loop once with a click on push button

조회 수: 2 (최근 30일)
Mralex Moe
Mralex Moe 2023년 4월 22일
댓글: Mralex Moe 2023년 4월 24일
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);

채택된 답변

chicken vector
chicken vector 2023년 4월 22일
편집: chicken vector 2023년 4월 22일
If I understood correctly, you want to extract one name from a list of names. In this case you don't need a loop.
At first glance you rcode seems correct, you just need to remove the loop because it creates the effect of clicking the Run loop button for Count times.
My suggestion is to initialise some variables outside the function extracting the lucky name.
% All this must be outside extraction (Run loop)
% Read the table only once outside of the extraction (right now you are
% reading the table every time you call 'Run lopp'):
handles.T = readtable('Name List.xlsx');
% Initialise list of names available:
handles.A = 1 : height(T);
% Initialise zero counter:
handles.counter = 0;
Then you can pass the info in the extraction function that, after the extraction, automatically updates the list of names left.
% Update counter:
handles.counter = handles.counter + 1;
% Extract random number from 1 to number of names availables:
extractedNumber = handles.A(randi(length(handles.A))); % No need of using randperm
% Remove extracted number from list of availables:
handles.A(handles.A == extractedNumber) = [];
% Rest of your code:
Lucky_Name = handles.T(R,:);
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);
guidata(hObject, handles);
  댓글 수: 1
Mralex Moe
Mralex Moe 2023년 4월 24일
Thanks for helping me Mr.chicken vector . It's working.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by