Plotting cell of different sized vectors

7 views (last 30 days)
Hanna Weiss
Hanna Weiss on 2 Dec 2019
Edited: Adam Danz on 4 Dec 2019
Hi guys,
I have a cell such that:
The cell index (1,2,3...) corresponds to a timeline.
Each cell element holds a vector (of different sizes) with several values indicating dominant frequencies at that time.
I wish to plot a graph in which the x axis is the timeline (the cells' indexes), and for each "time" to display dots showing all dominant frequencies of that time.
The problem is that there is a different number of "dominant frequencies" for each time.
I also want to plot the dots for each time in different colors specifying how strong the corresponding frequency is at that time.
Is there any way to do this?
Thanks!

Answers (1)

Adam Danz
Adam Danz on 2 Dec 2019
Edited: Adam Danz on 3 Dec 2019
You could use arrayfun()
Here's a demo. C is the cell array containing vectors, h is the handles to each line object.
C = {[2 4 6 8 10], [6 5 4], [0 1], 10:20};
axes()
hold on
h = arrayfun(@(i)plot(C{i},'o'),1:numel(C));
grid on
[Update] following comments below that clarified the problem.
C is the cell array containing the y-values. h is the handles to each line object.
C = {(1), [1 2] [1 2 3 4 5] };
clf()
axes()
hold on % important to hold axes prior to plotting
h = arrayfun(@(i)plot(i,C{i},'o'),1:numel(C));
Note, if C has any empties, you'll either need to replace the empties with NaN or remove them prior to plotting.
C = {(1), [1 2] [ ] [1 2 3 4 5] };
C(cellfun(@isempty,C)) = {nan}; %This preserves the x-index value
C(cellfun(@isempty,C)) = []; %This does not
  2 Comments
Hanna Weiss
Hanna Weiss on 3 Dec 2019
Hey :)
Thanks for the answer!
I did not explain well - I do not want all vectors to correspond to the same timeline.
I want for every "time" (as in every x-axis index) to plot the corresponding vector vertically.
Say I have the cell: { [1] [1 2] [1 2 3 4 5] }
Then I want at x=1 to plot a dot at y=1,
and at x=2 to plot 2 dots at y=1,2
and at x=3 to plot 5 dots at y=1,2,3,4,5...
is there any way to do this?
Thanks!!
Adam Danz
Adam Danz on 3 Dec 2019
Edited: Adam Danz on 4 Dec 2019
Sure, see updated answer. Again, using arrayfun.

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!