Friday, February 27, 2015

Post #5. Some ascii art made in MATLAB

ASCII art is a text based artistic representation. It is possible to find incredible examples of ASCII art in the internet. There is lot of methods to create them. I developed an automated code to extract the spatial information of color from a picture file and insert it in a plot with some characters. Below, some examples of ASCII arts that I made in MATLAB codes. The basic idea of the ASCII art codes in MATLAB is to use a function named imread ( ). This function read .jpg, .bmp or .png files as matrix. After that,  I used a function named text( ) to plot the character using the color information from the graphical matrix. The code is too long, but below I wrote the fundamental steps:

I = imread(File); %This line read the picture as a matrix. File is the name of the picture address in your computer.

%The lines below shows how to plots the characters (in the example is the character 'x') using the colot information from the picture file. a and b variables are the units of spacing between the characters in the x and y axis, respectively.
[numrow,numcol] = size(I);
for i = 1:a:numrow
    for j = 1:b:numcol
            text(j,i,'x','Color',[I(i,j,1)/255 I(i,j,2)/255 I(i,j,3)/255],'FontName','Arial', 'FontSize',8);
   end
end





All the best,

Givago (givagosouza@gmail.com)

Saturday, January 17, 2015

Post #4. Drawing in MATLAB.

An interesting thing that MATLAB helped me was to observe the world as a matrix. Everything can be represented by a set of numbers. In the current posting, I want to show some codes to draw shapes. The more important point for most of the examples is to find coordinates of specific points that delimitate the shape you want to draw. The Figure 1 shows an example of a shape (triangle) with the three coordinates (in red) that can be used as reference to draw it.
Figure 1. Example of triangle drawn using MATLAB. The coordinates from three points were enough to be used to draw it.

We can read the code to draw a triangle as follows. The coordinates from x axis and y axis must be organized in the sequence of the drawing.

%CODE #1
a = 0;
b = 0.5;
c = 1;
x1 = [a b c a]; % x coordinates
y1 = [a c a a]; % y coordinates
plot(x1,y1,'-')
axis([-4 4 -4 4])

Now, a code to draw a square!

%CODE #2
a = 0;
b = 0.5;
x1 = [a a b b a]; % x coordinates
y1 = [a b b a a]; % y coordinates
plot(x1,y1,'-')
axis([-4 4 -4 4])

Now, a code to draw irregular shape!

%CODE #3
a = 1;
b = 2;
c = 4;
x1 = [a a b c b a];
y1 = [a b c b b a]
plot(x1,y1,'-')
axis([-4 4 -4 4])

Figure 2. The triangle, square and irregular shape drawn using the previous codes (Codes #1, #2, and #3, respectively).

An exception!! Circles!! Circle could be drawn after we find many, many, many coordinates, but it will spend lot of time of us!! It is easier such as follows. 

%CODE #4
ang = 0:(2*pi/40):2*pi; %Sequence of 40 angles between 0 and 2 times pi
radius = 2; % Radius of the circle
x1 = cos(ang)*radius;
y1 = sin(ang)*radius;
plot(x1,y1,'-')
axis([-4 4 -4 4])
axis square

Figure 3. Circle drawn using the code #4.

Contact me to discuss about more codes. All the best.

Givago
givagosouza@gmail.com






Sunday, October 26, 2014

Post #3. Sinusoidal functions and Sinusoidal gratings

One issue of my interest have been to programm visual stimuli in order to use in vision research. Classically, a very useful stimulation for visual system investigations is named sinusoidal grating. The sinusoidal gratings are the graphical representation of the luminance or color modulation following a sinusoidal function. Here, I'll show how to program sinusoidal functions and its correspondent sinusoidal grating.
First, let's create sinusoidal function with the code below. It is important to set the free parameters of the sinusoidal functions: frequency, amplitude, and phase.

frequency = 1;%Set the frequency
phase = 90;%Set the phase in degrees that needs to be converted to radians
amplitude = 1;%Set the amplitude
x = 0:0.001:1;%x domain 
wave = amplitude*sin((2*pi*frequency*x)+(deg2rad(phase)));
plot(x,wave,'k','Linewidth',2)
axis square 
Fig 01. Graphical representation of the sinusoidal function set in the code above (frequency = 1; phase = 90 deg; amplitude = 1). 

Now, I'll show how to use the sinusoidal function to modulate the spatial luminance to create sinusoidal gratings. The code below use introduces the functions meshigrid( ) and surf( ). The function meshgrid( ) are used to create a square matrix that will be used to be modulated by the third dimension, the spatial luminance. The surf( ) function is used to make three dimensional shaded surface plot.

%We set the same previous parameters
frequency = 1;
phase = 90;
amplitude = 1;
[X,Y]=meshgrid(0:0.001:1,0:0.001:1);
Z = amplitude*sin((2*3.1415*frequency.*X)+(phase));
surf(X,Y,Z)
shading interp
view(0,90)
colormap gray
axis off
axis square
Fig 02. Luminance spatial sinusoidal grating for the parameters of the code above (frequency = 1; phase = 90 deg; amplitude = 1). Look the luminance is following the profile of the plot in the Figure 01.

Now, try to change the parameters to visualize other several sinusoidal waveforms. Below, more examples of sinusoidal functions and its correspondent sinusoidal grating. To know better about meshgrid( ) and surf( ) functions read the MATLAB help. 

           
            


frequency = 4, phase = 0; amplitude = 1               frequency = 10, phase = 270; amplitude = 1 




Any doubt, contact me in givagosouza@gmail.com

Saturday, October 18, 2014

Post #2. Choosing the file address to import datafiles

Someone, who works with stored data and needs to import new datafiles from different folders every time, needs to know how to import datafiles from specific folder address. In MATLAB, there is a function that help us with the task to looking for the desired folder. The function is uigetfile( ). This function opens the standard dialog for retrieving files. Specially for begginners, I'll indicate a code that introduces the use of this function.
The code starts with:

[file,dir] = uigetfile('.txt','Select file to import');


This line will make appear a open standard dialog box as showed below in the Fig 01.

Fig 01. Open standard dialog box. The title of the dialog box ('Select file to import') and the type of file ('.txt') to be imported were set in the command line above.

Once you chose the file to be imported, the function returns the value to the variables [file,dir]. The variable file will save the file name, and the variable dir will save the folder address. In the previous example, the variable file will be 'example.txt' , and the variable dir will be 'C:\Users\hp\Desktop\ '. Both information together will constitute the complete address of the file. The next line will concatenate the variables dir and file. 

FileToOpen =  [dir file];

The line above concatenated the both variables with [dir file], and the variable FileToOpen will receive the complete address of the file ('C:\Users\hp\Desktop\example.txt' ) to be imported.

The next lines will be used to evaluate if the variable FileToOpen is not valid. If it is ok, a new variable named Data will receive the information of the import command importdata( ) of the file addressed in FileToOpen. Finally the stored data will be ploted.

if FileToOpen ~= 0
    Data = importdata(FileToOpen);
    plot(Data(:,1),Data(:,2),'o')
else
end

This simple code can also be used to import different datafiles such as .csv and .mat. Any doubt contact me in givagosouza@gmail.com

  



Sunday, October 12, 2014

Post #1. High quality plot in MATLAB

It's my first post in this blog. My purpose is to open a link of communication with other people that wants to get more experience in the use of MATLAB programming for technical and scientific reasons or even just for fun. My first issue to discuss is how ugly or pretty can be the plots created in MATLAB. During the last seven years, I always thought that it would be impossible or I would spent lot of energy to make pretty plots using MATLAB. I used to process the data in MATLAB. After that, I exported the processed results to text file, and finally, I made the plots in Microsoft Excel. Obviously, I was doing something wrong! 
Today I'm posting a code to customize plots in MATLAB in order to make them more beautiful.
Look the plot below. This is one example of ugly plot made by using the default properties of MATLAB.

 Fig 01. An example of plot made using the default plot properties of MATLAB. Ugly plot!

The plot in the Fig 01 was created using the following simple code:

x = 1:10;       %Data from x domain
y = x+2;        %Data from y domain
plot(x,y,'o');  %Function to plot the data
axis square   %Command to make squared plots 
xlabel('X')    %Command to name the x domain
ylabel('Y')    %Command to name the y domain

Look now the same data with some changes in plot properties.
Fig 02. An example of a plot with the same data in the Fig 01, but with changes in many plot properties.

The Fig 02 was made using the still simple code:

x = 1:10; %Data from x domain
y = x+2; %Data from y domain

plot(x,y,'o','MarkerEdgeColor',[0.1 0.1 0.1],'MarkerFaceColor',[0.8 0.8 0.8],'MarkerSize',12); %Function to plot the data. Here, I change plot properties of the color of the maker edge and marker %face and the marker size.

axis square

%Two lines below, I set the font name, font size, and the font weight. I did using my preferences. 
xlabel('X','FontName','Times New Roman','Fontsize',18,'Fontweight','bold')
ylabel('Y','FontName','Times New Roman','Fontsize',18,'Fontweight','bold')

%Below, I changed many plot properties that in my opinion give more visibility to the graph
set(gca,'Box', 'off', 'TickDir', 'out', 'TickLength'  , [.035 .035] , 'XMinorTick'  , 'on', ...
'YMinorTick', 'on', 'YGrid', 'on', 'XGrid', 'on', 'LineWidth', 3, 'FontName', 'Times New Roman',...
'Fontsize',18,  'Fontweight','bold');

After you write the code to customize the plot, it is necessary only to apply the same code for new plots. I know that the beauty is in the eye of the beholder, and it doesn't matter if the Fig 02 is prettier than Fig 01, but with few more lines in your code you can change the quality of your data presentation.

Feel free to contact me about this post or other MATLAB issues by the e-mail givagosouza@gmail.com