error:Cell elements must be character arrays.

42 views (last 30 days)
Hi ,
I am running a loop : ISIN(i,1)=cusip2isin('US',Cusip91(i)); % I obtained cusip2isin from the file exchange forum. Cusip91(i)='05978R107' when i=1 and gives ISIN(1)='US05978R1077' without any error. but for i=2 , CUSIP91(2)=[463347104] and gives an error: Error using char Cell elements must be character arrays.
Error in cusip2isin (line 25) cusip=char(cusip);
I am wondering how to get rid of this error? I am not very familiar with char and cell arrays. Any help is greatly appreciated

Accepted Answer

Image Analyst
Image Analyst on 19 Jun 2015
Why is cell #1 a string, '05978R107', while cell #2 is a double scalar,[463347104]? Evidently that File Exchange function wants a cell that contains a string, not a cell that contains a double. You can convert to a string doing something like
if isnumeric(CUSIP91{i})
% Contents of cell are a number.
% Extract number, convert to a string
% then stick back into a cell.
thisCell = {num2str(CUSIP91{i})};
else
% The cell already contains a string so nothing to do.
thisCell = CUSIP91(i);
end
% Now call with a cell that has a string inside of it.
ISIN(i,1)=cusip2isin('US', thisCell);
For a good intuitive explanation of cells, see the DAQ: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
  2 Comments
AHawk
AHawk on 10 Jul 2017
Hello I am using this answer on an empty cell I created and am still getting an error message. My code is as follows
t1 = 1;
[~,y] = size(Data);
for k = 1:length(Data)
NewMatrix = cell(1,y);
if isnumeric(NewMatrix{1i})
thisCell = {num2str(NewMatrix{1i})};
else
thisCell = NewMatrix(1i);
end
char(NewMatrix);
But I am getting the following error message
'Subscript indices must either be real positive integers or logicals.' for this line of code 'if isnumeric(NewMatrix{1i})'
Image Analyst
Image Analyst on 10 Jul 2017
1i is the imaginary variable "i" = sqrt(-1). You cannot use this as an array index.
In this line:
if isnumeric(NewMatrix{1i})
And why should NewMatrix have anything in it when you just created it? It won't, it will be a row vector of "y" empty cells.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion 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!