While loop stops before meeting conditions

4 次查看(过去 30 天)
Hi guys,
I am using a while loop with 3 conditions. Here it is :
while (X<90 && Y<90 && Z<90)
...
Or the program stop when I reach these values :
X = 87.55
Y = 87.58
Z = 93.21
Only one condition is true, so why the program stops ?
Thank you very much for your help !
  3 个评论
Torsten
Torsten 2020-4-6
As written, the program leaves the while loop if at least one condition is false - and this is the case for Z.
Thibaut
Thibaut 2020-4-6
Thank you for your answer, here is my code. I use the program to run Ansys in batch mode.
I first launch a code to have a reference value then I do a while loop (written in Bold below).
% clean-up Matlab's workspace
% ---------------------------
clc
clear
% define some usefull variables
% -----------------------------
KEYWORD = 'Nb_Modes'; %Mot clef rechercé dans le code APDL et qui sera modifié
openMPStackSize = 'SET KMP_STACKSIZE=2048k';
pathToANSYS = '"C:\Program Files\ANSYS Inc\ANSYS Student\v194\ansys\bin\winx64\ANSYS194.exe"'; %Chemin d'accés
workingDirectory = ['"' pwd '"'];
templateScriptName1 = 'APDL.txt'; %Nom du fichier original
jobName = 'TEST';
inputFileName = [jobName '.lgw'];
outputFileName = [jobName '.out'];
fullCommand = [openMPStackSize ' & ' pathToANSYS ' -dir ' workingDirectory ' -b -j ' jobName ' -i ' inputFileName ' -o ' outputFileName];
Nombre_Modes = 50 %Variable utilisée
% clean-up the current directory
% ------------------------------
delete([jobName, '.*']);
templateScriptContents = fileread(templateScriptName1);
% replace keywords in the template script
% ---------------------------------------
inputFileContents = strrep(templateScriptContents, KEYWORD, num2str(Nombre_Modes));
%save the configured script to disk
% ----------------------------------
fileId = fopen(inputFileName, 'w+');
fprintf(fileId, '%s', inputFileContents);
fclose(fileId);
% run ANSYS
% ---------
[status, cmdout] = dos(fullCommand, '-echo');
%Lecture fichier résultats
%S=fileread('Results.txt')
A = regexp(fileread('Results.txt'),'\n','split'); %lecture du fichier contenant les résultats
whichline = find(contains(A,' sum |')); %Caractére à rechercher
% replace keywords in the template script
Modif=A(whichline); %Sélection de la ligne contenant les résultats
Modif1=erase(Modif,"sum"); %Suppression des caractéres
Modif2=erase(Modif1,"|");
%ascii value comparison, char(48) = '0' and char(57) = '9'
X=str2double(regexp(Modif2,'\d+[\.]?\d*','match','once')); % where c is your cell array; %Selection du paramètre X
X_str=num2str(X); %Transformation en string
Modif3=regexprep(Modif2,X_str,""); %Nous retirons le paramètre X
DEL1=str2double(regexp(Modif3,'\d+[\.]?\d*','match','once')); % where c is your cell array; %Nous sélectionnons le prochain paramètre pour ensuite le retirer
DEL1_str=num2str(DEL1,'%2.2f');
Modif4=regexprep(Modif3,DEL1_str,"");
Y=str2double(regexp(Modif4,'\d+[\.]?\d*','match','once')); % where c is your cell array;
Y_str=num2str(Y);
Modif5=regexprep(Modif4,Y_str,"");
DEL2=str2double(regexp(Modif5,'\d+[\.]?\d*','match','once')); % where c is your cell array
DEL2_str=num2str(DEL2,'%2.2f');
Modif6=regexprep(Modif5,DEL2_str,"");
Z=str2double(regexp(Modif6,'\d+[\.]?\d*','match','once')); % where c is your cell array
disp(X)
disp(Y)
disp(Z)
fclose('all')
while (X<90 && Y<90 && Z<90)
Nombre_Modes = Nombre_Modes + 1
% clean-up the current directory
% ------------------------------
delete([jobName, '.*']);
templateScriptContents = fileread(templateScriptName1);
% replace keywords in the template script
% ---------------------------------------
inputFileContents = strrep(templateScriptContents, KEYWORD, num2str(Nombre_Modes));
%save the configured script to disk
% ----------------------------------
fileId = fopen(inputFileName, 'w+');
fprintf(fileId, '%s', inputFileContents);
fclose(fileId);
% run ANSYS
% ---------
[status, cmdout] = dos(fullCommand, '-echo');
%Lecture fichier résultats
%S=fileread('Results.txt')
A = regexp(fileread('Results.txt'),'\n','split');
whichline = find(contains(A,' sum |'));
% replace keywords in the template script
Modif=A(whichline);
Modif1=erase(Modif,"sum");
Modif2=erase(Modif1,"|");
%ascii value comparison, char(48) = '0' and char(57) = '9'
X=str2double(regexp(Modif2,'\d+[\.]?\d*','match','once')); % where c is your cell array;
X_str=num2str(X);
Modif3=regexprep(Modif2,X_str,"");
DEL1=str2double(regexp(Modif3,'\d+[\.]?\d*','match','once')); % where c is your cell array;
DEL1_str=num2str(DEL1,'%2.2f');
Modif4=regexprep(Modif3,DEL1_str,"");
Y=str2double(regexp(Modif4,'\d+[\.]?\d*','match','once')); % where c is your cell array;
Y_str=num2str(Y);
Modif5=regexprep(Modif4,Y_str,"");
DEL2=str2double(regexp(Modif5,'\d+[\.]?\d*','match','once')); % where c is your cell array
DEL2_str=num2str(DEL2,'%2.2f');
Modif6=regexprep(Modif5,DEL2_str,"");
Z=str2double(regexp(Modif6,'\d+[\.]?\d*','match','once')); % where c is your cell array
disp(X)
disp(Y)
disp(Z)
end

请先登录,再进行评论。

采纳的回答

Srivardhan Gadila
Srivardhan Gadila 2020-4-10
As mentioned by @Torsten, the program remains in the while loop "while (X<90 && Y<90 && Z<90)" only when the values of all the variables X, Y & Z are less than 90. Since the value of Z is 93.21 which is greater than 90, hence the program exits the while loop.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by