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

Sunday, 27 January 2013

Matlab Commands: Input/Output and Formatting Commands

Input/Output Commands

disp Displays contents of an array or string.
fscanf Read formatted data from a file.
format Controls screen-display format.
fprintf Performs formatted writes to screen or file.
input Displays prompts and waits for input.
; Suppresses screen printing.

Format Codes for fprintf and fscanf

%s Format as a string.
%d Format as an integer.
%f Format as a floating point value.
%e Format as a floating point value in scientific notation.
%g Format in the most compact form: %f or %e.
\n Insert a new line in the output string.
\t Insert a tab in the output string.


Saturday, 26 January 2013

Matlab Commands: Commands for Managing a Session

Clc
Clears Command window.
clear Removes variables from memory.
exist Checks for existence of file or variable.
global Declares variables to be global.
help Searches for a help topic.
lookfor Searches help entries for a keyword.
quit Stops MATLAB.
who Lists current variables.
whos Lists current variables (long display).

Related links:

Matlab Commands: Operators and Special Characters


+
Plus; addition operator.
-
Minus; subtraction operator.
*
Scalar and matrix multiplication operator.
.*
Array multiplication operator.
^
Scalar and matrix exponentiation operator.
.^
Array exponentiation operator.
\
Left-division operator.
/
Right-division operator.
.\
Array left-division operator.
./
Array right-division operator.
:
Colon; generates regularly spaced elements and represents an entire row or column.
( )
Parentheses; encloses function arguments and array indices; overrides precedence.
[ ]
Brackets; enclosures array elements.
.
Decimal point.
Ellipsis; line-continuation operator.
,
Comma; separates statements and elements in a row.
;
Semicolon; separates columns and suppresses display.
%
Percent sign; designates a comment and specifies formatting.
_
Quote sign and transpose operator.
._
Nonconjugated transpose operator.
=
Assignment (replacement) operator.

Sunday, 17 June 2012

Embedding a watermark image into a cover image using Matlab program

Find below the Matlab codes to embed a watermark image into a cover(host) image:

clear all; close all; clc;

[filename1,pathname]=uigetfile('*.*','Select the host image'); 
image1=imread(num2str(filename1));
imshow(image1); title('Original image');% orginal image for watermarking

[ori_row,ori_col]=size(image1)
host_length=ori_row*ori_col;

i=1;
j=1;
k=1;

[filename2,pathname]=uigetfile('*.*','Select the watermark image'); 

wmimage=imread(num2str(filename2));
[wm_row,wm_col] = size(wmimage);
imshow(wmimage); title('Watermark image');

wm=dec2bin(wmimage);
wm_length=wm_row*wm_col*8;

host=dec2bin(image1);

counter=0; 

while i < host_length
        
        counter=counter+1;
        
        if counter > wm_length
            break;
        end
        
        host(i,8)=wm(j,k);
        k=k+1;
        
        if k>8
            k=1;
            j=j+1;
        end
        
        i=i+1;
end

    key1=wm_row
    key2=wm_col
   
im1=bin2dec(host);
im1=reshape(im1,ori_row,ori_col);
image1(1:ori_row,1:ori_col)=im1(1:ori_row,1:ori_col);

display 'After embed';

imwrite(image1,'wmarked.bmp'); % saves the watermarked image as a bmp file
imshow(image1);title('Watermarked image');

Saturday, 16 June 2012

Some Simple Matlab Exercises - Code Samples I

Some Simple Matlab Exercises - Code Samples I

You may find some Matlab Commands here - http://jayitsecurity.blogspot.com/2013/01/matlab-commands-operators-and-special.html

Write a pseudocode and matlab program which receive a positive integer value as input and then output the following message “Even Number” or “Odd Number”.

Example:

Input:           Enter a number: 7
Output:        Odd number

Input:           Enter a number: 8
Output:        Even number

Answer:

Pseudocode

Start
Read input Num
                x = Modulus (Num, 2)
                If x equals 0
                                then display Num is an even number
                Else
                                display Num is an odd number
End


Matlab program
Num = input('Enter a positive integer number: ')

x = mod(Num,2);

if x == 0
    sprintf('%d is an even number', Num)
   
else
    sprintf('%d is an odd number', Num)
   
end

Sample outputs

Enter a positive integer number: 5
ans =
5 is an odd number

Enter a positive integer number: 8
ans =
8 is an even number

Write the expression of matlab language which is equivalent with the following mathematical expressions:

a.     b– 4ac
b.     (a + b) (c + d) (e + f)

Answer:

a.     b*b – 4*a*c
b.     (a + b)*(c + d)*(e + f)


A chemist uses the decision table below to determine whether a solution is very acidic, acidic, neutral, alkaline, or very alkaline. Write a program that will read in the value of pH for a solution and display its type.

Table 1

pH range
Liquid types
>12
8 – 12
7
3 – 6
<= 2
very alkaline
alkaline
neutral
acidic
very acidic

Answer:

clear all, clc

pH = input('Enter pH value: ');

if pH>12
            sprintf('Liquid Type: Very Alkaline')

elseif pH>=8 & pH<=12
            sprintf('Liquid Type: Alkaline')

elseif pH==7
            sprintf('Liquid Type: Neutral')

elseif pH>=3 & pH<=6
            sprintf('Liquid Type: Acidic')

else
            sprintf('Liquid Type: Very Acidic')

end

Sample outputs

Enter pH value: 1
ans =
Liquid Type: Very Acidic

Enter pH value: 11
ans =
Liquid Type: Alkaline

Enter pH value: 7
ans =
Liquid Type: Neutral


Write a program which accepts input time in second and then output it in hour, minute and second.


Example:

Enter time in second: 3770
3770 seconds = 1 hour 2 minutes 50 seconds.

Answer:

Matlab program

clear all, clc;

InputSeconds = input('Enter time in seconds: ');

Seconds = mod(InputSeconds,60);

Bal_Seconds = InputSeconds - Seconds;

Hours = int8(Bal_Seconds/3600);

Minutes = int8(mod(Bal_Seconds,3600)/60);

sprintf('%d second(s) = %d hour(s) %d minute(s) %d second(s)',InputSeconds,Hours,Minutes,Seconds)

Sample outputs

Enter time in seconds: 3770
ans =
3770 second(s) = 1 hour(s) 2 minute(s) 50 second(s)

Enter time in seconds: 7281
ans =
7281 second(s) = 2 hour(s) 1 minute(s) 21 second(s)

Enter time in seconds: 325
ans =
325 second(s) = 0 hour(s) 5 minute(s) 25 second(s)


Popular Posts