error - 'Conversion to double from cell is not possible.'- in a for-loop

i have a loop like this:
x=cell(5,1);
for i = 1:5
x{i} = myfunction(z(i))
end
the x as the output from the function is from the type double. why do i get the error shown above?
thank you!

1 件のコメント

Stephen23
Stephen23 2016 年 12 月 29 日
@Payjay: you have not given us enough information to know exactly what the problem is. It is quite likely the problem is inside the function, not in this posted code. Please post a comment here giving the complete error message. This means all of the red text.

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

回答 (1 件)

Star Strider
Star Strider 2016 年 12 月 29 日

0 投票

The code you posted should work, even if the output is an array.
What is ‘z’? Is it a double vector or a cell array?
The problem may be in your ‘myfunction’ function. It may be attempting to use a cell array element ‘z(i)’ as a real value. It is possible to do this, but it requires specific cell array indexing, or conversion to double using the cell2mat function.

4 件のコメント

Payjay
Payjay 2016 年 12 月 29 日
z(i) are several directories. the function has many more output arguments, but by trying this one i get the error, so i think it has to do smth. with the output. but thanks anyway!
Star Strider
Star Strider 2016 年 12 月 29 日
By convention, specifying only one output from a function will return only the first output in the output list.
I guess we are not going to be getting any more information about ‘myfunction’, so I will delete my Answer in a few hours.
Payjay
Payjay 2016 年 12 月 29 日
編集済み: Payjay 2016 年 12 月 29 日
'myfunction' for this output x just ckecks how many subdirectories are in the directory and returns x as a double for numbering. sorry! for this output the funktion is only x = a*(1:numel(Dir)).
Star Strider
Star Strider 2016 年 12 月 29 日
If you output ‘x’ directly, what is it? What class and size are ‘Dir’?
Note that numel will return the total number of elements in ‘Dir’. If you want the number of rows, for example, use the size function, specifically size(Dir,1).
Consider using cellfun to get the size (or numel) results if your current code does not produce the result you want.
For example:
x = cellfun(@numel, Dir);
or:
x = cellfun(@(x) size(x,1), Dir);
Note This is UNTESTED CODE. I cannot guarantee that it will work in your ‘myfunction’ function.

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

カテゴリ

ヘルプ センター および File ExchangeEnvironment and Settings についてさらに検索

質問済み:

2016 年 12 月 29 日

編集済み:

2016 年 12 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by