how do i choose material in command window?

12 次查看(过去 30 天)
I am trying to write a program where the material can be chosen and a graph will be plotted for stress against strain. i am having difficulty choosing the material in the command window however. Can anyone point out where my mistake is?
select_material=input('Choose a plate material,Steel (st),Aluminium Alloy (al)')
if select_material==st
load('material_data_strain_st.txt');
load('material_data_stress_st.txt');
p=polyfit(material_data_strain_st.txt,material_data_stress_st.txt,1)
elseif select_material==Al
load('material_data_strain_al.txt');
load('material_data_stress_al.txt');
p=polyfit(material_data_strain_al.txt,material_data_stress_al.txt,1)
else
fprintf('invalid entry')
end
Cheers,

回答(1 个)

Geoff Hayes
Geoff Hayes 2014-11-14
Mike - if you are requesting that the user enter a string, then you need to include the 's' option so that the input is not evaluated and is treated as a string. If you run your code as is, you are probably observing the
Error using input
Undefined function or variable 'st'.
when you type in st. Instead change the input call to
select_material=input('Choose a plate material,Steel (st),Aluminium Alloy (al): ','s');
Now, select_material will be a character array (or string) and you can compare the selection to the strings st and al. In your code, you are doing this as
if select_material==st
But since st is undefined and select_material is a string then this evaluation will fail. Instead use strcmpi to compare strings
if strcmpi(select_material,'st')
% do stuff
elseif strcmpi(select_material,'al')
% do other stuff
else
fprintf('invalid material %s\n',select_material);
end
Try the above and see what happens!
  2 个评论
mike lynch
mike lynch 2014-11-16
Thanks Geoff, I think that worked! I am now getting a message saying "attempt to reference field of non structure array" but I think that has to do with the files I'm trying to load. Thanks for the help, Mike
Geoff Hayes
Geoff Hayes 2014-11-17
Mike - you must have some code that is trying to access data within an object using the period. i.e. something like
x = 42;
x.myData
The above generates the same error. Just look in your code for where you are doing something similar.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by