Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Convert 2D plot to binary (0/1)

Subject: Convert 2D plot to binary (0/1)

From: Robert

Date: 6 Jan, 2013 20:24:08

Message: 1 of 5

Hello,

I have a problem. I've tried for so long to convert 2D plot to binary (0 - when no line; 1 - when there is line) and save it to .txt file. By sampling values the plot should be 15 numbers height and 64 numbers width. Can anyone help me?

It should look like this example:

 000000111000000000000000000000000000000000000000000000000000000
 000001000100000000000000000000000000000000000000000000000000000
 000010000010000001100000000000000000000000000000000000000000000
 000010000010000010010000111111111111111111111111111111111111111
 000100000001000100001001000000000000000000000000000000000000000
 000100000001000100000110000000000000000000000000000000000000000
 001000000000111000000000000000000000000000000000000000000000000
 001000000000000000000000000000000000000000000000000000000000000
 001000000000000000000000000000000000000000000000000000000000000
 010000000000000000000000000000000000000000000000000000000000000
 010000000000000000000000000000000000000000000000000000000000000
 010000000000000000000000000000000000000000000000000000000000000
 100000000000000000000000000000000000000000000000000000000000000
 100000000000000000000000000000000000000000000000000000000000000
 000000000000000000000000000000000000000000000000000000000000000

Subject: Convert 2D plot to binary (0/1)

From: Roger Stafford

Date: 7 Jan, 2013 05:29:08

Message: 2 of 5

"Robert " <disciples7@wp.pl> wrote in message <kccmh8$tt$1@newscl01ah.mathworks.com>...
> I have a problem. I've tried for so long to convert 2D plot to binary (0 - when no line; 1 - when there is line) and save it to .txt file. By sampling values the plot should be 15 numbers height and 64 numbers width.
- - - - - - - - -
  Suppose x and y are row or column vectors of equal length and used in plot(x,y). To get the above kind of array do this.

 m = 15; n = 64;
 xmax = max(x); xmin = min(x);
 ymax = max(y); ymin = min(y);
 A = zeros(m,n);
 A(m*round((n-1)*(x-xmin)/(xmax-xmin)) + ...
     round((m-1)*(ymax-y)/(ymax-ymin))+1) = 1;

This should work on all such plots except single vertical or horizontal lines. I'll let you do the save to a txt file.

Roger Stafford

Subject: Convert 2D plot to binary (0/1)

From: Robert Nowak

Date: 9 Jan, 2013 00:40:13

Message: 3 of 5

So the whole code should be like this:

m = 15; n = 64;
xmax = max(x); xmin = min(x);
ymax = max(y); ymin = min(y);
A = zeros(m,n);
A(m*round((n-1)*(x-xmin)/(xmax-xmin)) + ...
        round((m-1)*(ymax-y)/(ymax-ymin))+1) = 1;
fid = fopen('example.txt', 'w');
fprintf(fid,'%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d...
              %d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d...
              %d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d...
              %d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d\n', A');
fclose(fid);
open('example.txt')

64 of "%d" => I needed to transpose whole array A and write in sequence 64 numbers and go to another line and so on (in order to save it in .txt file).
Therefore I've got another question. Is there any possibility, to avoid this long line of "%d"?

Subject: Convert 2D plot to binary (0/1)

From: Roger Stafford

Date: 9 Jan, 2013 03:15:08

Message: 4 of 5

"Robert Nowak" <disciples7@wp.pl> wrote in message <kcie9d$m8p$1@newscl01ah.mathworks.com>...
> Therefore I've got another question. Is there any possibility, to avoid this long line of "%d"?
- - - - - - - -
  Try this:

 for k = 1:size(A,1)
  fprintf(fid,'%1d',A(k,:)), fprintf(fid,'\n')
 end

Roger Stafford

Subject: Convert 2D plot to binary (0/1)

From: Arkadiusz Kowalski

Date: 11 Jan, 2013 08:07:09

Message: 5 of 5

Thank you for your help. It worked.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us