I have a 1by2 cell array that has the following elements
C{1}= [ 1 2 3 4 5 6]
C{2}= [987 877 77 666 777 66 77 ]
I want to plot these matrices C{1} and C{2} on the same graph.
Pleas help.

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 3월 7일

0 개 추천

You can plot elements of a cell array on the same graph by holding the axes. For example,
C{1} = [1 2 3 4 5 6];
C{2} = [987 877 77 666 777 66 77];
fig = figure();
ax = axes();
hold(ax);
for i = C
plot(i{:});
end

댓글 수: 7

shahzer rahman
shahzer rahman 2020년 3월 7일
Hey Ameer, this code works perfectly fine when the X-axes starts from zero for both the cells.
However, the X-axes for C{2} in my case starts from 10.
I am sorry, I forgot to mention this in the original question. Can you help with that?
Ameer Hamza
Ameer Hamza 2020년 3월 7일
How is your data available. Do you have seperate cell array for the x-coordinates and y-coordinetes?
For example, If you have two arrays Cx and Cy for x-coordinates and y-coordinates respectively then you can try following
Cx{1} = [1 2 3 4 5 6];
Cx{2} = [10 11 12 13 14 15 16];
Cy{1} = [1 2 3 4 5 6];
Cy{2} = [987 877 77 666 777 66 77];
fig = figure();
ax = axes();
hold(ax);
for i = 1:length(Cx)
plot(Cx{i}, Cy{i})
end
shahzer rahman
shahzer rahman 2020년 3월 7일
I have cell arrays for Y-coordinates. I am figuring out a way to make cell arrays for the X-coordinate too.
Ameer Hamza
Ameer Hamza 2020년 3월 7일
If you have both cell arrays, then you can use the code in the above comment.
shahzer rahman
shahzer rahman 2020년 3월 7일
How do I find the index of the last element of each cell ?
For example; C{1}= [1 2 3 4 5 6]; and C{2} = [987 877 77 666 777 66 77];
I need the index of 6 from C{1} and that of 77 from C{2}.
expected answer,
index_end_C1=6
index_end_C2=7
Ameer Hamza
Ameer Hamza 2020년 3월 7일
편집: Ameer Hamza 2020년 3월 7일
You can get the index of last element using this
cellfun(@(x) numel(x), C)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2020년 3월 7일

편집:

2020년 3월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by