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

26 vues (au cours des 30 derniers jours)
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".
  1 commentaire
Walter Roberson
Walter Roberson le 12 Avr 2016
Why did you ask the same question three times? Now someone has to go in and try to merge the three different versions :(

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 12 Avr 2016
Modifié(e) : Stephen23 le 12 Avr 2016
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.
  7 commentaires
Stephen23
Stephen23 le 12 Avr 2016
Modifié(e) : Stephen23 le 12 Avr 2016
inpC = {'Name_1','Name_2','Name_3','Name_4','Name_5';...
23.5, 25.8, 29.6, 24.6, 29.6;...
24.5, 85.5, 59.2, 75.1, 1000};
%
pairs = {'Name_1','Name_2';...
'Name_3','Name_5';...
'Name_2','Name_4'};
%
[~,Z] = ismember(pairs',inpC(1,:));
out = reshape(inpC(:,Z),3,2,[])
and this is the output:
out(:,:,1) =
'Name_1' 'Name_2'
[ 23.5] [ 25.8]
[ 24.5] [ 85.5]
out(:,:,2) =
'Name_3' 'Name_5'
[ 29.6] [ 29.6]
[ 59.2] [ 1000]
out(:,:,3) =
'Name_2' 'Name_4'
[ 25.8] [ 24.6]
[ 85.5] [ 75.1]
As is usually the case with MATLAB, the task can be solved much simpler without any loops.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by