Info
此问题已关闭。 请重新打开它进行编辑或回答。
Why do I get different responses when I run a piece of inside a function or directly in the command window?
1 次查看(过去 30 天)
显示 更早的评论
When I run the following piece of code (directly in the command window, for debugging purposes):
categories_hash = arrayfun(@(idx) string2hash(categories{idx}), 1:size(categories));
- it works
but when I invoke the function that contains it, I get the following error:
Cell contents reference from a non-cell array object.
Error in intellegite>@(idx)string2hash(categories{idx}) (line 22)
categories_hash = arrayfun(@(idx) string2hash(categories{idx}), 1:size(categories));
Error in intellegite (line 22)
categories_hash = arrayfun(@(idx) string2hash(categories{idx}), 1:size(categories));
8 个评论
dpb
2017-5-25
Kewl~the debugger is most powerful tool, indeed.
Demonstrates that context is important; was sure to point of near certainty that was the problem. Matlab rarely is wrong in its error diagnosis of this type... :)
回答(1 个)
Walter Roberson
2017-5-25
You have
vec_cat = '';
then you have a double-nested loop that will not necessarily execute the inner code at all (because the input could be empty.)
Then you unique() this item that might still be '', after which you try to use the result of that as if it were a cell array.
You should be initializing vec_cat = {};
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!