Info

This question is closed. Reopen it to edit or answer.

I have 5 buttons in a code, and I want to create a function "escreve" that, if he presses the button 3 he uses the function"escreve" and if he presses the button 4 he does another thing

1 view (last 30 days)
!!! This is our buttons!!! (they are in a menu)
case 3
moo=1;
escreve(matriz,moo(1), assignment, nme, custo);
case 4
moo=2;
escreve(matriz,moo(2), assignment, nme, custo);
& I want to when he presses the button 3 moo=1 and it gives "escreve" moo and if moo=1 it does the first part) and if he presess 4 ....
!!! This is our function "escreve" !!!
function [] = escreve(matriz,moo, assignment, nme, custo)
if moo == 1
fprintf('Custo total --> %.2f', custo);
[~, numeroTrabalhadores] = size(matriz);
trinca = length(assignment);
for a = 1: numeroTrabalhadores
custo=matriz(a,assignment(a));
fprintf('\ntrabalhador %d---> tarefa %d (custo %.2f)',a,assignment(a),custo);
end
end
else moo == 2
escreve(matriz,moo, assignment, nme, custo)
[nome1, nome2] = strtok(nme, '.');
nome1 = [nome1, '_res']; % adiciona ao nome1 que ficou "_res"
nome3 = [nome1, nome2]; % junta o nome1 e nome1 criando o nome3
[~, numeroTrabalhadores] = size(matriz); % criar uma matriz com as
% afectações
qlqr = length(assignment);
fileID = fopen(nome3,'w');
fprintf(fileID,'Afectações e custos');
fprintf(fileID, '\n-------------------');
fprintf(fileID, '\nCusto total --> %.2f', custo);
for a = 1: numeroTrabalhadores
custo=matriz(a,assignment(a));
fprintf(fileID,'\nTrabalhador %d---> Tarefa %d (custo %.2f)',a,...
assignment(a),custo);
end
fclose(fileID); % guarda e fecha o ficheiro
end
my function escreve needs to do either the first part if moo=1 and the second one if moo=2
i want to if he presses case 3 it does the first
and if he presses case 4 it does the second part
i tried to creat a variable and give it to the function but its not working
i know if i separate both and give diferent names they work but they need to be 1 function
the first part of escreve writes this on the command window
"Afectações e custos
-------------------
Custo total --> 33.00
Trabalhador 1---> Tarefa 5 (custo 3.00)
Trabalhador 2---> Tarefa 3 (custo 3.00)
Trabalhador 3---> Tarefa 1 (custo 1.00)
Trabalhador 4---> Tarefa 4 (custo 23.00)
Trabalhador 5---> Tarefa 2 (custo 3.00)"
and the second part he separates the name that user gave from its extension txt adds" _res" and combines them all to create a file, and then prints in that file the first part.

Answers (1)

Image Analyst
Image Analyst on 8 Dec 2018
"if he presses the button 3 he uses the function"escreve" and if he presses the button 4 he does another thing" <=== Try this:
case 3
moo=1;
% Call escreve() function.
escreve(matriz,moo(1), assignment, nme, custo);
case 4
% Call anotherThing() function.
anotherThing(variable1, variable2);
Of course you have to write the "another thing" function.

Community Treasure Hunt

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

Start Hunting!