hi i want to ask,why matlab cannot process the condition for the while loop ? Thank you!

1 view (last 30 days)
clc
clear
close all
%Input data
w=input('Enter the load,w: ');
L=input('Enter the beam length: ');
x=input('Enter specific point: ');
%While loop if false
while (w<1||L<1||x<0||isempty(w)||isempty(L)||isempty(x))
fprintf("\nPlease recheck input data\n")
w=input('Enter the load,w: ');
L=input('Enter the beam length: ');
x=input('Enter specific point: ');
end
%true input data
Ra=w*L;
Ma=-w*L.^2/2;
Mx=-w*(L-x).^2/2;
Sx=w*(L-x);
fprintf("\nThe reaction at A is %.2f\n",Ra)
fprintf("The moment at A is %.2f\n",Ma)
fprintf("The bending moment at point %.f is %.2f\n",x,Mx)
fprintf("The shear force at point %.f is %.2f\n",x,Sx)
  2 Comments
Image Analyst
Image Analyst on 10 Jan 2022
Works fine for me. What are your initial inputs before the loop and then what are you entering in the loop?
NUR AIN ATIKAH ABDUL LATIF
i want to make the process re-input the data if user key-in wrong input like negative value and no input from user

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 10 Jan 2022
Since you're using the short-circuiting or operator you should check the size of the inputs first. Rather than asking if they're empty, ask if they're not a scalar.
%{
while ~isscalar(w) || ~isscalar(L) || ~isscalar(x) || w < 1 || L < 1 || x < 0
%}
That way if (for example) w was not a scalar the ~isscalar(w) check would make the while condition return false before it gets to the w < 1 part (and throws an error.)
w = 0:5;
~isscalar(w) || w < 1 % false, MATLAB doesn't get to the w < 1 check
ans = logical
1
w < 1 || ~isscalar(w) % error, w < 1 is logical but not scalar
Operands to the logical and (&&) and or (||) operators must be convertible to logical scalar values.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!