我正在尝试纠正这段代码,其中用户将为 a 和 b 输入值,但 b 必须大于 a。因此代码会对其进行测试,如果出现错误回复,对话框询问用户他想重新输入哪个值,用户将单击一个按钮,该按钮应将他引导至另一个应询问他的对话框对于 a 或 b 的值,然后检查输入的这个新值。我需要将这些值作为数字出来,以便以后进行一些计算,但我的代码无法正常工作,这是我目前所拥有的:
if str2num(b) <= str2num(a); %convert value to doulbe to run the test
promptMessage = sprintf('The value of the b is smaller or equal to the value of a, do you want to reinput the value of the a or the value of the b?' );
button = questdlg(promptMessage, 'Reinput value of a or b ?', 'a', 'b', 'Rod-Crank');
if strcmpi(button, 'a')
prompt = ('Please reenter the value of a:');
a=inputdlg(prompt);
while isnan(str2double(a)) || ~isreal(str2double(a)) || isinf(str2double(a)) || str2double(a) <= 0;
prompt = ('Please reenter the value of a:');
a=inputdlg(prompt);
end
c = char(a);
a=str2num(c(1));
b=str2num(b);
end
if strcmpi(button, 'b')
prompt = ('Please reenter the value of b:');
b=inputdlg(prompt);
while isnan(str2double(b)) || ~isreal(str2double(b)) || isinf(str2double(b)) || str2double (b) <= 0;
prompt = ('Please reenter the value of b:');
b=inputdlg(prompt);
end
c = char(b);
b=str2num(c(1));
a=str2num(a);
end
end
if isnan(str2double(f)) || ~isreal(str2double(f)) || isinf(str2double(f)) || isempty(f) || str2double (f) < 0; % test if the value of c is valid
promptMessage = sprintf('The value entered for the Frequency " %0.5g " is incorrect, Please reenter the value for Frequency ', f);
button = questdlg(promptMessage, 'Error in value of the Frequency.', 'Ok', 'Ok');
end
f = str2num(f);
for t = 0 : 1 : 360;
Time = t+1;
O = (2.*pi.*f)./60;
i = b.^2-a.^2.*((sind(t)).^2);
j = (a.^2).*(O).*sind(2.*t);
dt (Time) = a.*cosd(t)+ sqrt(i);
V (Time) = (-a.*O.*sind(t))-(j/(2.*(sqrt(i))));
A (Time) = (-a.*(O.^2).*cosd(t))-(4.*(a.^2).*(O.^2).*cosd(2.*t).*i + j.^2)/(4.*(i.^1.5));
y (Time) = t;
end