Search row with string to find column number

2 views (last 30 days)
I have created a matrix with row 1 full of strings and 4 other rows with numbers. They are created in a handle class with the object Projekter.
So in the object Projekter row 1, the first value is blank, but the second value is 'Ole'. So I know that 'Ole' is in column 2. x is the name/string I want to search for, which in this case is 'Ole'.
As you see below it should search row 1 from column 2 untill the end and if i = 'Ole', it should bring me the value 2 because i should be equal 2.
How do I fix this so I can get the i value when the statement is correct?
A is just a controller if the function works, but at this point it doesn't.
function number(obj,x)
A = [];
for i = 2:size(obj.Projekter,2)
if obj.Projekter(1,i)==x
A = A + 1;
end
end
disp(A)
end

Answers (1)

Walter Roberson
Walter Roberson on 10 Jun 2015
Is obj.Projekter a cell array of strings? If so then
[tf, A] = ismember(x, obj.Projekter);
tf will be false for elements of x that do not occur in obj.Projector. "A" will be the indices in obj.Projekter where the matches were found.
  1 Comment
Andreas Kristensen
Andreas Kristensen on 10 Jun 2015
obj.Projekter is a cell array of strings yes.
Matlab still makes an error when I enter your code. It doesn't recognise the tf. It also give the error:
Undefined function 'eq' for input arguments of type 'cell'.
Error in P/number (line 46)
if obj.Projekter(1,i)==navn

Sign in to comment.

Categories

Find more on Numeric Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!