How to resolve "Conversion to double from cell is not possible."

187 ビュー (過去 30 日間)
James Carter
James Carter 2013 年 5 月 1 日
コメント済み: Walter Roberson 2018 年 9 月 27 日
Can someone point out what I am doing wrong. If I use the internals of the following function in command-line, the code works fine, but when I attempt to execute through the function, I get the error listed above listed for line 12. The offending code is: NewArry(3:LenB+2) = varargin;
function [maxVal, maxInd] = inner_product(varargin)
% varargin contains the accumulated signal returns
K = [2 3 10 2 1];
LenB = length(varargin);
EndL = varargin(1);
EndR = varargin(LenB);
%Pre-allocate New Array
NewArry = zeros(1,LenB + 4);
NewArry(3:LenB+2) = varargin;
NewArry(1) = EndL;
NewArry(2) = EndL;
NewArry(LenB + 3) = EndR;
NewArry(LenB + 4) = EndR;
FinalArry = zeros(1:LenB); %Preallocate array to hold inner product values
for x = 1:LenB
%Take the first group of 5 from the NewArray
Y = NewArry(x:x+4);
%Take The Inner Product of this with the Kernel
IP = Y * K';
%Store this value
FinalArry(1,x) = IP;
end
[maxVal, maxInd] = max(FinalArry);
end
When I assign varargin, and then pass it to the function, [maxVal, maxInd] = inner_product(varargin) it fails.
Thanks
  1 件のコメント
Walter Roberson
Walter Roberson 2018 年 9 月 27 日
FinalArry = zeros(1:LenB);
is incorrect. It would try to create a 1 x 2 x 3 x 4 x.... LenB array.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2013 年 5 月 1 日
varargin is a cell array, and you cannot assign a cell array to a numeric array. Look again at your statements
NewArry = zeros(1,LenB + 4);
NewArry(3:LenB+2) = varargin;
  2 件のコメント
James Carter
James Carter 2013 年 5 月 1 日
varargin is a 1 x 1021 vector, not a cell array, so I am not sure where it is picking up that classification. Besides, why does this work when I run the bare code outside the function but then only triggers the error when I run the exact same code within the function? Attempting to use the cell2mat routine then results in a dimensional mismatch.
Thanks
JC
Walter Roberson
Walter Roberson 2013 年 5 月 1 日
No, varargin{1} might be a 1 x 1021 vector, but varargin will always be a cell array.
Outside of a function, varargin references the script that is just the help information for varargin, and would present an error if used in a context that requires a value. varargin inside a function is special.
foo = @(varargin) class(varargin)
now try
foo([1 2 3])
and
foo('hello', 83)
and notice that each time the result is "cell".

サインインしてコメントする。

その他の回答 (2 件)

Babak
Babak 2013 年 5 月 1 日
use
cell2mat()

David Weuffel
David Weuffel 2018 年 9 月 27 日
編集済み: David Weuffel 2018 年 9 月 27 日
for everybody still searching for answers to this:
it is also possible, that in some scenarios you are trying to convert a char array eg.:
'word'
'2.345'
try using
str2double();

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by