Does anyone know what function to use to plot a space time graph?? (for the Nagel-Schreckneberg model, Graph displayed)

6 views (last 30 days)
Here, I am trying to redraw this graph on MATLAB. The dots represent 0 velocity while the integers represent velocity. Which functions shall I use to draw this graph?

Accepted Answer

Star Strider
Star Strider on 5 Dec 2014
This brings me back to my FORTRAN days when we did such plots on the line printer!
The idea is to set up an array of ‘.’ and then overwrite numbers on the string array to do the plot:
s = repmat('.',1,72);
v = [20 5; 60 5; 72 5]; % Create Data
for k1 = 1:5
sd = s;
v(:,1) = rem(v(:,1)+1, 72);
sd(v(:,1)) = num2str(v(:,2),'%d');
fprintf(1, '%s\n', sd)
end
I created a ‘v’ matrix whose first column are the positions along the ‘sd’ vector, and whose second column are the velocities. The first line (‘sd’) resets the string for that loop iteration. The second line just increments ‘v’. (In your application, you will likely have calculated your own matrix of positions and velocities. This is just an illustration.) The next line (with num2str) prints the velocities at the correct position on the ‘sd’ vector. The last line prints it.
Run it to see what it does and how it works. This should get you started.
  2 Comments
Samuel Abera
Samuel Abera on 15 Dec 2014
thanks for the help, I have managed to reproduce the matrix but I have two issues. One is, all the ouputs are produced in one long row, how can I get to start a new line for a new loop? The second issue is, How can I replace the 0's to .'s?
Star Strider
Star Strider on 15 Dec 2014
My pleasure.
On Windows machines, the '\n' newline character string starts a new line. (Run my example code for an illustration.) See the documentation for fprintf under ‘Input Arguments’ -> ‘formatSpec’ -> ‘Control Characters’ for details.
I’ve addressed your second issue in my example code as well. See the ‘s’ string assignment at the top for details. If you want to print a period (.), it’s just another character, specifically '.', like any of the others.
By the way, my example code assumes a 72-character fixed-width-character line. That’s the reason for the repmat call.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 5 Dec 2014
How about fprintf()?
Or else scatter() or plot(), to do the dots, along with text() to do the numbers.

Tags

Community Treasure Hunt

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

Start Hunting!