简体   繁体   中英

Plotting vectors in matlab

I have this situation and I can't solve it in the proper way. The problem is this: I have 3 vectors:

  • Vector1 = [name1 name2 name3 name4 ... nameN] (string names)
  • Vector2 = [time1 time2 time3 time4] (Double)
  • Vector3 = [time1:name4 time2:name1 time3:name1 time4:name1] (double:String)

I want to do the following in matlab:

1- Put Vector 1 in Y axis with names - I could do it with this code:

   set(gca, 'YTick',1:N, 'YTickLabel',Names(:,1))

2- put Vector 2 in X axis with, to simulate time line

3- Once we have both axis X&Y I'd like to use the 3 Vector to plot point in the graph

For example, 3 Vector contains secuentially timestamps and in each timestamp is executed the nameN, so I'd like to plot a dot in the graph using 3 vector as input.

Any suggestion?Thanks in advance

You need to convert the names in vector3 to numbers, then you can call the plot command.

For example

names = {'a','b','c','d'}; %# use a cell array (curly brackets) for strings
time = [10 20 30 40 50];
data = {10,'d';20,'b';40,'c'}

%# convert data to numeric xData, yData
xData = cell2mat(data(:,1));
[dummy,yData] = ismember(data(:,2),names);

%# plot
plot(xData,yData,'.') %# plot dots
set(gca,'YTick',1:length(names),'YTickLabel',names,'XTick',time)

%# make sure the axes limits aren't too tight
xlim([0,60]),ylim([0,5])

One way to do it is,

  1. put only the values in Vector3
  2. then use plot(vector2, vector3)

Also I suggest to rename vector1 to "scale", vector2 to "time", and vector3 to "values". That should help to get your mind clear about what you are using with what etc. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM