run loop once with a click on push button
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mralex Moe
le 22 Avr 2023
Commenté : Mralex Moe
le 24 Avr 2023
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);
0 commentaires
Réponse acceptée
chicken vector
le 22 Avr 2023
Modifié(e) : chicken vector
le 22 Avr 2023
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);
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!