GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Showing posts with label Matlab. Show all posts
Showing posts with label Matlab. Show all posts

Wednesday, December 22, 2010

Invoking Java From Matlab

You can build a Java application using Matlab:

>> java
>> f = javax.swing.JFrame('A Java Application');
>> f.setSize(400,300);
>> f.setLocation(200, 100);
>> f.setVisible(true);

Then you can add some attribute
>> layout = java.awt.FlowLayout();
>> f.getContentPane().setLayout(layout);

>> bExit = javax.swing.JButton("Exit");
>> f.getContentPane().add(bExit);

          
Share/Bookmark

Defining Function In Matlab

Firstly, type this code and then save it - addTwo.m
function output = addTwo(input)
% This function will add the input with 2
output = 2 + input;

In Matlab interactive mode, type:
>> addTwo(3)
ans 5

The function also can have more than one input and output arguments. Save this code to addThen.m:
function [add,dot] = addThen(input)
% This function will add and also will multiply 
% the input arguments.
add = input + input;
dot = input * input;

Then in Matlab IDE, type
>> [a,b] = addThen(4)
a = 8
b = 16
  
    
Share/Bookmark

If Else In Matlab

Type to test:

>> sleep = true;
>> if sleep
disp('Do sleep');
end
Do sleep

>> a = 2;
>> if a>3
disp('Nothing');
else
disp('Do sleep')
end
Do sleep


Share/Bookmark

Looping In Matlab

For do looping in Matlab, type:
>> for i=0:20    % do looping for 0 till 20
disp(i)
end

>> for i=0:2:20   % do looping for 0 till 20 step 2
disp(i)
end

     
For while looping, do:
>> i = 0;
>> while (i<10)
disp(i)
i = i + 1;
end 
       
Share/Bookmark

Basic Graphics In Matlab

To create a graphics, type:

>> figure
>> x = 0:0.1:2*pi;
>> y = sin(x);
>> plot(x, y);

Miscelaneous commands:
>> close       % Close the active graphics window
>> close all   % Close the graphics window
>> clf         % Clear the window from the graph
>> hold on     % Keep the line in the previous display
>> hold off    % Lose the line in the previous display
>> grid        % Toggle for grid
>> cla         % Clear axis
>> box         % Toggle box
>> rotate3d    % Make the graph so can moving like a 3D objects
            
Share/Bookmark

Data Analysis Using Matlab

Creating a matrix by type:
>> data = [2 3;
           4 5;
           8 3];

>> max(data)   
8 5

>> min(data)
2 3

>> mean(data)
4.6667 3.6667

>> median(data)
4 3

>> sort(data)
2 3
4 3
8 5

>> sum(data)
14 11

>> data<5
1 1
1 0
0 1
        
Share/Bookmark

Matrics In Matlab

To create a matric, type:
>> data = [1 2;
           3 4] 

>> data(1,2)    % Get the cross of the 1st row and the 2nd column
2               % var(row, col)

>> data(end,end)   % Get the end of row and column
4

>> data(1:2,1)  % Get elements cross at rows 1 till 2 and col 1
1
3

>> data(1:2,1:2)   % Get rows 1 till 2 and col 1 till 2
1 2                % var(r1:r2, c1:c2)
3 4                % r1:r2 = row1 till row2

>> data(:,1)    % Get all rows at col 1
1
3

>> data(1,:)    % Get all rows at row 1
1 2

>>  data(:)    % Get all elements in consecutive order

Detele rows or columns:
>> data(1,:)    % Delete all elements at the first row
>> data(:,1)    % Delete all elements at the first column

Generating built in matric style:
>> zeros(r,c)    % Creating zero matrix by r and c dimension
>> magic(d)      % Creating random matrix by d and d dimension
>> ones(r,c)     % Creating 1 matrix by r and c dimension
>> rand(r,c)     % Creating random matrix by r and c dimension
>> randn(r,c)    % Creating random matrix (negative included) 
                 % by r and c dimension
>> eye(d)        % Creating a diagonal matrix
          
Share/Bookmark

Vector In Matlab

In Matlab, data structure vector and matrix:
>> id = [4 5 6 2 4]
4 5 6 2 4
>> id = 1:5    % start:end
1 2 3 4 5
>> id = 1:2:10    % start:step:end
1 3 5 7 9

How to access elements in matrix:
>> id = [4 5 6 7 8]
4 5 6 7 8
>> id(1)    % Get the 1st element of the id
4
>> id([1 2 3])    % Get the 1st, 2nd, 3rd elements of id
4 5 6
>> id(1:3)    % Get the 1st till the 3rd elements
4 5 6
>> id(end)    % Get the last element
8
          
Share/Bookmark