Input and output dynamic program

If your program before was not interactive, not dynamic, and rigid impressed because every time you want to use the new data you should change its data in the script, you can just make it dynamic.

MATLAB provides the facility to be able to interact directly with the program without having to change the script. To ask for input from the user, MATLAB provides input function. Syntax of writing as follows:

variable = input ( 'string display');

and to display program output to the screen, MATLAB provides the function of Disp. Syntax of writing as follows:

Disp ( 'string is displayed')

Here is an example of writing to receive input syntax is continued by displaying the results of the program to the screen:

1. In the command window, type:

>> Edit

2. Hit enter, then appeared MATLAB Editor and you type in the program follows:

clear all;
CLC;

Disp ('---------------------------');
Disp ( 'My 3th Training Program');
Disp ('---------------------------');

length = input ( 'length of data =');
width = input ( 'width data =');
area = length * width;
Disp ([ 'Area ->', num2str (area)]);

3. When finished typing the above programs, you save it in a directory c: / mytraining, with the name training03.m

4. Ensure your file storage directory is contained in the list MATLAB directory search. You can learn how to do that in directory management article.  Then type the file name without the extension training03:

>> training03

5. Press Enter, then the program will run, in succession enter variable length and width and then hit enter and then hit enter and produce output as follows:

--------------------------
My 3th Training Program
--------------------------
length of data = 6
Data width = 2
Area -> 12
>>

6. Done

* Note:
At the end of the program code is num2str code. Num2str code serves to convert the data type of the original area of numerical type into string type. This is in order to be placed together with another string in the mark these brackets.

0 comments:

Post a Comment