MATLAB: Input A of class cell and input B of class cell must be cell arrays of strings, unless one is a string”.

errorforismemberloopmatlab2013

tickers='tickers.txt'; %list of tickers of financial stocks
price=hist_stock_data('09032015','09032016',tickers); %download data from yahoo.finanace
prices=zeros(size(price(1, 1).AdjClose,1),size(price,2));
for t=1:size(price,2);
prices(:,t)= price(1, t).AdjClose;
end
%I create an array in which I have a list of tickers called nomes
for t=1:size(price,2);
nomes{1,t}=price(1, t).Ticker;
end
%couple is a string array nx2 with all possible pairs obtained from the “nomes”
cl=num2cell(prices); %to obtain a cell
ff=[nomes;cl]; %to have a matrix in which the first row is the name of stocks, and over there the historical prices for each stocks
%Now I want to obtain a for loop in which for each pair extrapolate the historical price from the matrix “prices”, utilizing the function ismember.
for ii=1:size(couple,1);
looking_up=couple(ii,:);
[tf, coldix]=ismember(looking_up,ff(ii,:));
Prices=ff(:,coldix(tf));
prices=prices(2:end,:);
prices=cell2mat(prices);
x(ii)=prices(:,2);
y(ii)=prices(:,1);
end
but give me the following error: Input A of class cell and input B of class cell must be cell arrays of strings, unless one is a string".

Best Answer

The message seems quite clear: you are providing a cell array to ismember, but it is not cell arrays of strings. The function ismember accepts certain combinations of input variable classes, as its documentation explains:
" B must belong to the same class as A with the following exceptions:"
  • "logical, char, and all numeric classes can combine with double arrays."
  • "Cell arrays of strings can combine with char arrays."
  • ...
Read your code:
  • looking_up is defined with looking_up=couple(ii,:), and your notes say is "couple is a string array nx2". So looking_up is a char array.
  • ff is defined as ff=[nomes;cl]. It is not clear what class nomes has, but cl is defined from as cl=num2cell(prices), where prices=zeros(...), so cl is clearly a cell array of numerics (and not a cell array of strings!)
So you are trying to call ismember on:
  • looking_up, a char array.
  • ff, a mixed cell array containing something (char?) and numerics.
There is no case listed in the documentation that supports this combination. As the documentation states, ismember works on two cell arrays of strings, or a char array and a cell array of strings, or on two numeric arrays, or a numeric array and a char array, but not on this mixture that you are attempting.