One condition is not being fulfilled, and suddenly when i wrote on command window, give me a infinite menu, and i can not close it

1 view (last 30 days)
Hello, in this program that I'm building, when b1==2, there is no fulfillment of that condition, and when I write in the window command, later an infinite menu appears... I don't understand why, I've tried everything and I don't understand because my idea is not working.
So the problem begins when the user chose: 'Manually'
If u respond on input 'a1' : 2, it´s not giving what it´s in the code, it gives me the condition b1==1, and appears the menu that i talked earlier.
please help, the code it´s:
clc, clear, close
while true
a=menu({'This program allows the fitting of functions to experimental points.', ...
'To start the program, click on the Start button.'},'Start','Exit');
%Operation of menu a
if a==1
b= menu('How do you intend to insert the experimental points?', ...
'By a file','Manually','Back','Exit');
end
if a==2
break
end
%Operation of the menu b
if b==1
elseif b==2
a1= sprintf('If you want to advance in the program, press 1.\n If you want to go back to the previous menu, press 2: ');
b1= input(a1);
p = input('How many experimental values do you want to insert?: ');
if b1==1
while p < 2 && p <= 0
errordlg ({'The number of experimental points must be at least 2.','Please press Ok and enter the desired value in the Command Window.'})
p = input('How many experimental values do you want to insert?: ');
end
n = p/2; %Number of matrix rows
m = 2; %Number of matrix columns
a = zeros(n,m); %prelocation
for i=1:n
for j=1:m
if (j == 1)
fprintf('Enter the %0.0fᵒ ',i)
a(i,j)= input('value of x: ');
else
fprintf('Enter the %0.0fᵒ ',i)
a(i,j)= input('value of y: ');
end
end
end
a=reshape(a,n,m); %Matrix according to the number of rows and columns
disp(a); %Matrix according to the number of rows and columns
end
if b1==2
clc
b= menu('How do you intend to insert the experimental points?', ...
'By a file','Manually','Back','Exit');
else
disp('The number entered was not correct')
end
if b==3
a=menu({'This program allows the fitting of functions to experimental points.', ...
'To start the program, click on the Start button.'},'Start','Exit');
if b==4
break
end
end
end
end
  2 Comments
Stephen23
Stephen23 on 13 Jan 2023
One of the changes that VBBV did, is to align the code consistently. Poorly aligned code hides the structure of the code, which makes it harder to understand (i.e. increases the risk of bugs and makes it harder to debug). You should consistently align your code. Note that the MATLAB editor can do this for you: select all code, press ctrl + i
Madalena Francisco
Madalena Francisco on 13 Jan 2023
Wow and that´s amazing, I didn't know about that, thanks a lot Stephen! I have a Mac, where should i press? Thank u thank u!!

Sign in to comment.

Accepted Answer

VBBV
VBBV on 13 Jan 2023
Edited: VBBV on 13 Jan 2023
clc, clear, close
while true
a=menu({'This program allows the fitting of functions to experimental points.', ...
'To start the program, click on the Start button.'},'Start','Exit');
%Operation of menu a
if a==1
b= menu('How do you intend to insert the experimental points?', ...
'By a file','Manually','Back','Exit');
end
if a==2
break
end
%Operation of the menu b
if b==1
disp('Insert a file')
elseif b==2
a1 = input('If you want to advance in the program, press 1.\n If you want to go back to the previous menu, press 2: ');
b1 = a1 ;
if b1==1
p = input('How many experimental values do you want to insert?: '); % this statement
while p < 2 && p <= 0
errordlg ({'The number of experimental points must be at least 2.','Please press Ok and enter the desired value in the Command Window.'})
p = input('How many experimental values do you want to insert?: ');
end
n = p/2; %Number of matrix rows
m = 2; %Number of matrix columns
a = zeros(n,m); %prelocation
for i=1:n
for j=1:m
if (j == 1)
fprintf('Enter the %0.0fᵒ ',i)
a(i,j)= input('value of x: ');
else
fprintf('Enter the %0.0fᵒ ',i)
a(i,j)= input('value of y: ');
end
end
end
a=reshape(a,n,m); %Matrix according to the number of rows and columns
disp(a); %Matrix according to the number of rows and columns
elseif b1==2
clear b
b = menu('How do you intend to insert the experimental points?', ...
'By a file','Manually','Back','Exit');
else
disp('The number entered was not correct')
end
elseif b==3
a=menu({'This program allows the fitting of functions to experimental points.', ...
'To start the program, click on the Start button.'},'Start','Exit');
elseif b==4
break
end
end
  12 Comments
Madalena Francisco
Madalena Francisco on 13 Jan 2023
Edited: Madalena Francisco on 13 Jan 2023
Thank u so much again!! Wow! And I let the final if b==4?
elseif b1==2
clear b
b = menu('How do you intend to insert the experimental points?', ...
'By a file','Manually','Back','Exit');
if b == 4 % i added this one and it works!
break
end
else
disp('The number entered was not correct')
end
elseif b==3
a=menu({'This program allows the fitting of functions to experimental points.', ...
'To start the program, click on the Start button.'},'Start','Exit');
elseif b==4 % but i need this one too right?
% Or i need to put below every b menu to close the program?
break
end
end
Madalena Francisco
Madalena Francisco on 13 Jan 2023
Ok I think I understand... if I want the statments to work in each menu, their have to be below the menu is that it?

Sign in to comment.

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!