error:Cell elements must be character arrays.

40 次查看(过去 30 天)
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

采纳的回答

Image Analyst
Image Analyst 2015-6-19
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 个评论
AHawk
AHawk 2017-7-10
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 2017-7-10
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by