Trouble Matching Data in Indexed Array

Hey, everyone! I'm trying to create a program where the Array "Accepted" has a direct correlation to the Array "Values". For example "A" would be given value "1", "B", "2", and so on. and then at the end it asks how many units of the variable of "Accepted" and divides it by its given value. So if I said 7 units of "C" it would return "7/3 total C". So i created these arrays but i don't know how i would link them up that way. the way I have it I feel that it would just take the entire array rather than assigning each array a specific value of the other. ideas?
thanks!
Accepted = {'A','B', 'C'};
Values = {'1','2','3'} ;
letter = input('what letter? ','s');
if any(strcmpi(elmnt,Accepted))
disp ('\n')
else
disp ('that''s not going to work')
end
units = input ('how many units of it?');
fprintf('%d units of (Accepted)', units/(Values))

Answers (1)

You can get the index of the matched item easily:
index = find(strcmpi(elmnt,Accepted));
I find disp, and concatenation easier to handle than fprintf
disp([num2str(units) '/' Values{index} ' of "' elmnt{index} '".'])

5 Comments

oooh, it really didn't like
disp([num2str(units) '/' Values{index} ' of "' elmnt{index} '".'])
I changed the brackets to parentheses and it stopped complaining about cell arrays but there still seems to be something wrong.
What is the error string it's spitting out?
so now it's sending this out.
'4' '/' '1' ' of "' 'A' [1] '".'
it seems that no matter what letter I put in, the output is the same, omly with the first number changing based on "how many units" i enter. when i get rid of the '' around the division sign i get an error how mrdivide is invalid for type cells and then
Error in ==> thing1 at 12
disp([num2str(units) /
Values(index) ' of "' elmnt (index)
'".'])
>>
watcha think?
You're not using {} [braces] and () [parentheses] correctly.
i know, i know, but when i keep the braces i get.
"??? Cell contents reference from a
non-cell array object."
Error in ==> thing1 at 12
disp([num2str(units) '/'
Values{index} ' of "' elmnt{index}
'".'])
and then i'm lost. but i do appreciate all your input

Sign in to comment.

Asked:

on 10 Jun 2013

Community Treasure Hunt

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

Start Hunting!