Receiving error when trying to obtain multiple inputs
9 次查看(过去 30 天)
显示 更早的评论
I have three user inputs with three prompts before hand. I am trying to have the code ask a question, user inputs, assigns string input to a variable, and have that do it three times (once for each prompt). Although after entering a value for the first prompt, the code returns an issue with the second prompt. I am not sure what the issue is. Can someone help me out?
I have included the entire code, I am aware that the "if" statements are reptitive and could be optimized.
Below is the code for reference:
%Assuming using Glass FRP rated at 480 Mpa (Range: 480-1600 Mpa)
%Defining Variables (in MPA):
plastic_tensile = 23.00001401;
frp_tensile = 479.9999027;
%Recieve user inputs for material chosen, atmospheric pressure, and radius of opening:
prompt1 = ("Please enter the material you would like to test (a. plastic b. frp): ");
prompt2 = ("Input the atmospheric pressure you would like to test (in PSI): ");
prompt3 = ("Input the radius of the opening you would like to calculate (in meters): ");
picked_mat = input(prompt1, 's');
psi = input(prompt2, 's');
r = input(prompt3, 's');
pimatlwr = lower(picked_mat);
%Calculate response based on info
% F = P*A
%oparea meaning (Area of Opening)
if pimatlwr {"plastic"}
oparea = (pi * (r^2));
newtons = (psi * oparea);
nm2 = (newtons/oparea);
mpaarea = (nm2/1000000);
dataset1 = [mpaarea];
reply1 = ("The area of the opening would exert %2.0f on the sheet. The sheet can only withstand 23 MPA, thus it would break.");
reply2 = ("The area of the opening would exert %2.0f on the sheet. The sheet can withstand 23 MPA, thus it would not break.");
if mpaarea > plastic_tensile
text = fprintf(reply1, dataset1);
elseif mpaarea < plastic_tensile
text2 = fpritnf(reply2, dataset1);
end
end
if pimatlwr {"frp"}
oparea = (pi * (r^2));
newtons = (psi * oparea);
nm2 = (newtons/oparea);
mpaarea2 = (nm2/1000000);
dataset2 = [mpaarea];
reply1_1 = ("The area of the opening would exert %2.0f on the sheet. The sheet can only withstand 480 MPA, thus it would break.");
reply2_1 = ("The area of the opening would exert %2.0f on the sheet. The sheet can withstand 480 MPA, thus it would not break.");
if mpaarea > frp_tensile
text1_1 = fprintf(reply1_1, dataset2);
elseif mpaarea < plastic_tensile
text2_1 = fpritnf(reply2_1, dataset2);
end
end
0 个评论
采纳的回答
Voss
2022-12-16
编辑:Voss
2022-12-16
It's a good idea to share the complete error message you received, including the line number and the text of the line the error happened (all of which is reported to you in the error message itself).
Anyways, here's how you can fix some of the potential errors I see:
picked_mat = input(prompt1, 's');
% don't use 's' in input() with prompt2 and prompt3, since
% you want to store numeric values in psi and r:
psi = input(prompt2);
r = input(prompt3);
% ...
% this is not valid syntax:
% if pimatlwr {"plastic"}
% I imagine you mean:
if strcmp(pimatlwr,"plastic")
% ...
end
% then similarly for pimatlwr {"frp"} later on
if strcmp(pimatlwr,"frp")
% ...
end
% Or maybe you want to allow the user to type "plastic" or "a" to select
% plastic, etc.:
if strcmp(pimatlwr,"plastic") || strcmp(pimatlwr,"a")
% ...
end
% And watch out for "fpritnf" typos:
% text2 = fpritnf(reply2, dataset1);
text2 = fprintf(reply2, dataset1);
% text2_1 = fpritnf(reply2_1, dataset2);
text2_1 = fprintf(reply2_1, dataset2);
And I guess mpaarea in the second if block should be mpaarea2, but it's not clear that they can't both be mpaarea (depends on what other code not seen is doing). Just use the right name within a block.
2 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!