Converting logical to cell array
10 次查看(过去 30 天)
显示 更早的评论
My code works with data extracted by a different code. Here it is:
for i=1:length(TrialTimestamps) %loop through trials
start1=TrialTimestamps(i);finish1=LightOnTimestamps(i); %trial onset to light on
temp1=(start1<=PokeTimestamps)&(PokeTimestamps<=finish1);% counts the nose pokes in this epoch
if temp1 == 0
temp1(i) = 0;
end
numpokes1(1,i)=temp1;
end
This is the error I receive: Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 61-by-1.
If I don't have numpokes, then temp1 only has logical, which cannot be used by the rest of the code. I need it to be in cell. I don't know how to turn it into a cell, but also preserve the entirety of the data.
0 个评论
采纳的回答
Star Strider
2020-5-11
I do not understand if ‘PokeTimeStamps’ changes its size (only that it appeasrs to be a column vector), or for that matter, what your code is doing.
One possibility:
numpokes1{:,i}=temp1;
That puts it in the
column of cell array ‘numpokes’.
See if that does what you want.
2 个评论
Star Strider
2020-5-12
When I tested that approach with this code:
temp1 = rand(10,1)>0.5;
numpokes{:,1} = temp1;
it ran without error.
The sort error my code threw when you used it usually means that ‘numpokes’ (here) already exists as something other than a cell array. The same error appears with:
numpokes = zeros(10);
temp1 = rand(10,1)>0.5;
numpokes{:,1} = temp1;
However this runs without error:
numpokes = cell(1,1);
temp1 = rand(10,1)>0.5;
numpokes{:,1} = temp1;
as does this:
numpokes = cell(1,1);
for i = 1:5
temp1 = rand(10+i,1)>0.5;
numpokes{i,:} = temp1;
end
Preallocate it as a cell array and my code should run without error.
That should work.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!