how do i choose material in command window?

12 views (last 30 days)
mike
mike on 14 Nov 2014
Commented: Geoff Hayes on 17 Nov 2014
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,

Answers (1)

Geoff Hayes
Geoff Hayes on 14 Nov 2014
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 Comments
mike lynch
mike lynch on 16 Nov 2014
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 on 17 Nov 2014
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.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!