Reputation: 239
I'm relatively new to matlab, so this might be an easy question, which I apologize for.
I have a cell array with cells of various dimensions. Some are empty matrices (0x2 empty matrix, 0x3 empty matrix, 0x16 empty matrix...stuff like that), and some aren't empty matrices. I'm trying to plot the cell array like so:
for n = 1:numel(cellarray)
plot(cellarray{1}(n))
hold on
end
But because I have some empty matrices, I get an error message when I try to run this loop and plot.
Is there any way I can change the empty matrices in my cell array to zero matrices of the same dimensions so that I can plot it without an error message? Thanks so much for any and all help!
Upvotes: 0
Views: 114
Reputation: 101
You can use a nested for loop to implement a value of zero in each element of your cell array.
for z=1:m
for y=1:n
for x=1:l
Cellarray{x,y,z}=0;
end
end
end
where your matrix is lxn array of m cells.
Upvotes: -1
Reputation: 10772
All your code is doing - or trying to do - is printing the n-th element of the first element of the cell array. Since the first element of the cell array contains fewer than n elements you get the error message you show in the comments.
From your description is sounds as if you are expecting it to plot the n-th element of the cell array - which it is not doing.
You most likely want,
for n = 1:numel(cellarray)
if ~isempty(cellarray{n})
plot(cellarray{n})
hold on
end
end
Upvotes: 1
Reputation: 269
Fill your empty Cells with NaN's. Matlab won't plot NaNs. If you have everything the same size, or as double precision, that would be easier to understand as a beginner, FYI.
So before you put anything into your Cell called "cellarray" do this:
Cellarray = repmat({NaN},3,3); % Whatever size your cell is
Upvotes: 1