Plot and color curves depending on the condition

I have a cell array Ready, 4x500.
Example, how it looks.
Each cell is a double array, similar structure.
Example of Ready{1,2}.
I want to make a plot for every 5th row in Ready as x-axis, (6,7,8,9) and the sum of 3d row as y-axis(2.4443, 2.4443, 2.4443 in the example).
So, as a result I want to get 4 plots (for every row of cell array Ready), every cell - different color (black, read or green), depending on 7th row (001 - black, 111 - green, 000 - red).
Could you please help me with this?
Thank you very much!

 Accepted Answer

I am unsure what you mean by "he sum of 3d row as y-axis(2.4443, 2.4443, 2.4443 in the example)."
For now I am plotting the 5th row against the 3rd row. Please see below.
for i = 1:size(Ready,1)
figure
title(['Plot for Row ' num2str(i)])
xlabel('X Label')
ylabel('Y Label')
for ii=1:size(Ready,2)
m = Ready{i,ii};
if [m(7,2) m(7,3) m(7,4)] == [0 0 1]
plot(m(5,:),m(3,:),'color','k')
end
if [m(7,2) m(7,3) m(7,4)] == [1 1 1]
plot(m(5,:),m(3,:),'color','g')
end
if [m(7,2) m(7,3) m(7,4)] == [0 0 0]
plot(m(5,:),m(3,:),'color','r')
end
hold on
end
hold off
end

8 Comments

Thank you!
The third row sum vs 5th row: 6(x) vs 0(y), 7 vs (0+2.4443), 8 vs (0+2.4443+0), etc.
So, for each column of 3d row I need to sum up all previous values in the 3d row.
Also, some of the cells in Ready are just empty, how can I skip them, so MATLAb wont give an error?
for i = 1:size(Ready,1)
figure
title(['Plot for Row ' num2str(i)])
xlabel('X Label')
ylabel('Y Label')
for ii=1:size(Ready,2)
if ~isempty(Ready{i,ii})
m = Ready{i,ii};
if [m(7,2) m(7,3) m(7,4)] == [0 0 1]
plot(m(5,:),cumsum(m(3,:)),'color','k') %cumsum is cumulative sum
end
if [m(7,2) m(7,3) m(7,4)] == [1 1 1]
plot(m(5,:),cumsum(m(3,:)),'color','g')
end
if [m(7,2) m(7,3) m(7,4)] == [0 0 0]
plot(m(5,:),cumsum(m(3,:)),'color','r')
end
hold on
end
end
hold off
end
Thank you! How do I check the output (what goes to the plot), because all the figures are empty for some reason.
@Kevin Holly For some reason, cumsum doesn't work.
@CheshireM What error are you getting for cumsum?
You can use the new variables (e.g. fifth_row_black) to investigate.
%Preallocate Variables - this speeds up the runtime
fifth_row_black = zeros([size(Ready),250]); % I arbitrarily chose 250 here - I'm not sure what your longest row will be that you will plot
Third_row_black = zeros([size(Ready),250]);
fifth_row_green = zeros([size(Ready),250]);
Third_row_green = zeros([size(Ready),250]);
fifth_row_red = zeros([size(Ready),250]);
Third_row_red= zeros([size(Ready),250]);
for i = 1:size(Ready,1)
figure
title(['Plot for Row ' num2str(i)])
xlabel('X Label')
ylabel('Y Label')
for ii=1:size(Ready,2)
if ~isempty(Ready{i,ii})
m = Ready{i,ii};
if [m(7,2) m(7,3) m(7,4)] == [0 0 1]
fifth_row_black(i,ii,:) = m(5,:);
Third_row_black(i,ii,:) = cumsum(m(3,:));
plot(fifth_row_black,Third_row_black),'color','k') %cumsum is cumulative sum
end
if [m(7,2) m(7,3) m(7,4)] == [1 1 1]
fifth_row_green(i,ii,:) = m(5,:);
Third_row_green(i,ii,:) = cumsum(m(3,:));
plot(fifth_row_green,Third_row_green),'color','g')
end
if [m(7,2) m(7,3) m(7,4)] == [0 0 0]
fifth_row_red(i,ii,:) = m(5,:);
Third_row_red(i,ii,:) = cumsum(m(3,:));
plot(fifth_row_red,Third_row_red),'color','r')
end
hold on
end
end
hold off
end
Thank you so much! I learnt so much from you!
The error was that index in array cant be more than 1. But I was able to fix it to remove NAN in the row, just setting the option 'omitnan' for cumsum.
Thank you!
@CheshireM No problem! If you can, I would appreciate it if you would accept this answer.
Of course! Thank you!

Sign in to comment.

More Answers (0)

Products

Release

R2021a

Asked:

on 29 Sep 2021

Commented:

on 30 Sep 2021

Community Treasure Hunt

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

Start Hunting!