big =
Inf
EDU» big2=2^realmax
big2 =
Inf
16.9 Strings
• Not all real-world
objects are best represented by numbers
- e.g., names,
addresses, units of measurement
• A common type
supported by most languages (including Matlab) is the String
- a collection of
characters
• Indicated by the
single-quote '
EDU» str1='My first
string'
str1 =
My first string
EDU»
findstr(str1,'first')
ans =
4
EDU»
strcmp(str1,'My')
ans =
0
EDU»
strncmp(str1,'My',2)
ans =
1
EDU» str2='45.6'
str2 =
45.6
EDU» str2num(str2)
ans =
45.6000
EDU»
strcat(str1,str2)
ans =
My first string45.6
16.10 Managing Variables
• The variables
created during a Matlab session are not persistent objects:
- they cease to
exist when the session is over
- endure for the duration
of the session
• Matlab provides
the following commands for managing the workspace
16.10.1 clear
• delete a variable
now
EDU» x=7.9
x =
7.9000
EDU» clear x
EDU» x
??? Undefined
function or variable 'x'.
|
• Useful if you wish
to ensure that a variable starts with no value
- initialisation
16.11 Managing Workspace in Matlab: load
• Load in a variable
that has previously been saved from Matlab or another source
EDU» linear=1:10
linear =
1
2 3 4
5 6 7
8 9 10
EDU» save linear
EDU» clear linear
EDU» linear
??? Undefined
function or variable 'linear'.
|
EDU» load linear
EDU» linear
linear =
1
2 3 4
5 6 7
8 9 10
• Useful for large
and/or important data
16.12 Managing Workspace: save
• Save a matlab
variable to a file for usage in a later session
EDU»
squares=linear.^2
squares =
1
4 9 16
25 36 49
64 81 100
EDU» save mydata
linear squares
EDU» clear linear
squares
EDU» squares
??? Undefined
function or variable 'squares'.
|
EDU» load mydata
EDU» squares
squares =
1
4 9 16
25 36 49
64 81 100
16.13 Review
• Variables &
identifiers
• Data types in
Matlab
- implications
• Special Constants
& Variables
- inf & nan
• Complex numbers
• Strings
• Managing variables
17 I/O IN MATLAB
• All programming
languages possess instructions for obtaining input (generally from the user)
and outputting results
- without input a
program always computes the same result
- without output a
program is effectively useless
• Matlab has an
unusual mixture of I/o instructions
- a simple input and
output statement useful in most cases
- a set of more
complex functions heavily based on C’s fprintf & fscanf
• Today’s lecture
looks at:
- the syntax of
these statements
- how they can be
used to increase the functionality of programs
References: For Engineers (Ch. 1, 8)
User’s
Guide (Ch. 3, 4)
Mastering
(Ch. 3, 4, 5)
17.1 Basic Input
• Matlab has a
simple, single statement known as input that serves to both:
- prompt for an input and
- return what the
user read.
|
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Email: kursusmatlab@gmail.com 17.1.1 The Input Statement
• General syntax
<variable> =
input(prompt-string);
e.g.,
height=input(‘Enter
the initial drop height ‘);
meanWeight=80.0; % Mean weight of a single
%
aicrew member in kgs
crewWeight =
meanWeight * …
floor(input(‘How many air-crew today?
‘));
• Function:
- print the prompt-string on the
screen (without a return character)
- wait for the user to enter a
value at the keyboard
- place that value in the variable
on the left-hand-side of the expression
17.2 Inputting Strings
• By default the
input statement reads only numeric values
- see below regarding expressions
- must explicitly state we are
reading a string
<variable>=input(prompt,string-read);
e.g.,
name=input(‘Please
enter your name’,’s’);
17.3 Behaviour of Input Statement
• Consider the
following example:
>>
ex1=input('1st number...')
1st number...10
ex1 =
10
>>
ex2=input('2nd input...')
2nd input...34 45
??? 34 45
|
Unrecognized operand
or partial expression.
|
2nd input...34
ex2 =
34
>>
ex3=input('3rd input...')
3rd input...ex2*100
-ex1 + 7.6
ex3 =
3.3976e+03
• Will re-prompt
until acceptable input received
• Will accept
expressions in the Matlab language!
- shouldn’t seek to
exploit this fact (but how to stop users?)
17.4 Micro: Amoeba Expansion
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% micro.m
% A colony of micro
organisms grows at such a
% rate that its
population doubles after a
% specified number
of hours.The user supplies an
% initial population
size, name for the species,
% and interval
required to double in size and
% the script charts
the progress for the first
% 48-hours.
|
% A modification of
the amoeba script to
% illustrate the
utility of the input statement.
|
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Email: kursusmatlab@gmail.com % Author: Spike
% Date: 15/2/1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
species =
input('What is the organism''s name? ','s');
initialNum =
input('Please enter the initial number of species...');
doublePeriod =
input('Please enter the number of hours required for the population to
double...');
interval = 0:48; % Two days
population =
initialNum*2.0.^(interval/doublePeriod);
combined = [interval
; population];
disp(strcat(species,'
Organism Population'));
disp('Hour Population');
fprintf('%d\t%6.0f\n',combined);
plot(interval,population,'-+');
xlabel('Hours');
ylabel('Population');
17.5 Micro Run
>> micro
What is the
organism's name? little-us nastius
Please enter the
initial number of species...1e5
Please enter the
number of hours required for the population to double...5.34
little-us nastius
Organism Population
Hour Population
0 100000
1 113860
18 1034446
19 1177824
20 1341075
45 34416732
46 39187024
47 44618498
48 50802794
17.6 Basic Output
• Matlab has a simple
single statement called disp for displaying values
- displays literals and values of
variables
- format controlled by a separate
command
17.6.1 The disp Statement
• General forms:
disp(variable); OR
disp(expression);
e.g.,
>> disp(ex1);
10
>> disp('Text
such as for table headings');
Text such as for
table headings
>> disp('The
value of ex2=',ex2);
??? Error using
==> disp
Too many input
arguments.
|
>>
disp(strcat('10 times ex2 equals ',num2str(10*ex2)));
10 times ex2
equals340
• Displays only a
single argument
• Argument can be an
expression
17.7 The format command
• Matlab has a
default behavior when outputting numeric values:
- if value is a whole number then
display as an integer
- otherwise if there’s about 5
significant digits or less then display as a float
- otherwise display it in
scientific notation
• Matlab provides a
number of different possible formats for showing numeric values
- specified with the
format instruction
- applies for all
disp (and variable name) output until the format is changed with another
format instruction.
|
Command Explanation
format short 5 digits, also the default format
Format long 16 digits
format short e 5 digits in exponential form
format long e 16 digits in exponential form
format short g “better” of short or short e
format long g “better” of long or long e
format hex As a hexadecimal number
format bank To
2 decimal places (like currency)
format + Sign of value only
format rat As a rational approximation
17.8 Format Examples
• format
instructions should be used in conjunction with disp to ensure the output
answers are in the most appropriate form.
|
Note: format does
not change the actual value of the variable being displayed, it simply alters
the display format.
|
e.g.,
>> format short;
disp(pi);
3.1416
>> format
long; disp(pi);
3.14159265358979
>> format
short e; disp(pi);
3.1416e+00
>> format long
e; disp(pi);
3.141592653589793e+00
>> format
short g; disp(pi);
3.1416
>> format long
g; disp(pi);
3.14159265358979
>> format hex;
disp(pi);
400921fb54442d18
>> format
bank; disp(pi);
3.14
>> format +;
disp(pi);
+
>> format rat;
disp(pi);
355/113
17.9 more
• Matlab allows the
user to force paginated output using the more command
- very useful for
viewing long output from scripts or long help listings
more on
- turn paging on. Will present
output in “chunks” of a page at a time
- <space-bar> to get another
page
- <return key> to get an
additional line
more off
- turn paging off
more(n)
- set the page size
to be n lines
17.10 Files in General
• disp and input are
associated with the screen and keyboard, respectively
- output always to
the screen, input always taken from the keyboard
• Ideally should
also be able to read from and write to files:
- important or large
input data sets
- permanent record
of the results of the program
• Matlab provides
this service through imitation of C’s fopen, fclose, fprintf, & fscanf
instructions
17.10.1 Opening & Closing Files
fopen Open a file for reading or writing and
provide a descriptor (handle or pointer) for the file.
|
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Email: kursusmatlab@gmail.com
file-descriptor=fopen(name,mode);
e.g.,
infile=fopen(‘testdata.dat’,’rt’);
fclose Close an opened file.
|
fclose(file-descriptor);
e.g.,
fclose(infile);
17.11 fprintf
• Output arguments
to a file using the specified format
fprintf(file,format,values)
file – file
descriptor returned by fopen
format – string
specifying the format to use for individual arguments
values – comma
separated list of variables or expressions to be output
• Values are matched
with format specifications on a one-by-one basis
- implicitly vectorised: will
re-use the format specifications for additional values
• If no file
descriptor supplied goes to screen
>> x=5.7;
y=19.6; t='text';
>>
fprintf('x=%d, y=%f, t=%s\n',x,y,z);
x=5.700000e+00,
y=19.600000, t=a
>>
fprintf('x=%d, y=%f, t=%s\n',x,y,t);
x=5.700000e+00,
y=19.600000, t=text
>>
fprintf('x=%5.2f or does x=%e or does x=%i\n',x,x,x);
x= 5.70 or does
x=5.700000e+00 or does x=5.700000e+00
>>
fprintf('x=%5.2f or does x=%e or does x=%5.0f\n',x,x,x);
x= 5.70 or does
x=5.700000e+00 or does x= 6
>>
fprintf('%e\t',x,y);
5.700000e+00 1.960000e+01 >> diary off
17.12 Controlling fprintf
• Advantages over
disp
- exact control over format of
output
- vectoring makes tables etc. easy
without loops
combined = [interval
; population];
disp('Micro Organism
Population');
disp('Hour Population');
fprintf('%d\t%6.0f\n',combined);
• Some of the
important format/control statements include:
Control Function
%d Decimal notation
%c Single character
%s String
%e Scientific notation
%f Floating point notation
\t Insert a tab
\n Insert a newline
• Each format
specification may have a number inserted between the % sign and the letter
designator:
- how much space to consume for
output
- for floats can specify both
total number of characters and those that follow the decimal point
17.13 fscanf
• The input
corollary of fprintf is fscanf
• General syntax:
<variable>=fscanf(file,format,size)
file – file
descriptor
format – as for
fprintf
size – optional. The
number of elements to read. If no size then will consume the entire file.
|
• Values are matched
with format on a one-to-one basis
• If no file
descriptor is supplied it reads from standard input (the keyboard)
• Useful for
“sucking in” large data sets, but often load will do the same thing simpler.
|
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Email: kursusmatlab@gmail.com
e.g.,
response =
fscanf(‘%s’,1); % Simpler to
use
%
input
% A file called
temperatures.dat holding a
% number of
temperature values. Open the file
% and suck the
values (all of them) into a
% variable called temps
and find their mean.
|
tempFile =
fopen(‘temperatures.dat’,’rt’);
temps =
fscanf(tempFile,’%f’);
fclose(tempFile);
fprintf(‘The mean
temperature is %4.1f\n’,
mean(temps));
17.14 Projectile Example
A program to
calculate the flight path of a projectile (e.g., mortar shell) using the
standard equations of motion.
|
User will supply
initial muzzle velocity and angle of barrel inclination, together with how
many “snap-shots” of the shell’s flight are required.
|
Using the equations
of motion and basic geometry we derive:
Where
t is time
g is the acceleration due to
gravity
vi is the muzzle velocity
is the muzzle angle (to the
horizon)
ttotal is the total shell
flight time
dx is the horizontal distance
dy is the vertical distance
17.15 Projectile Code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% projectile.m
% A script to calculate the flight
path of a
% projectile (e.g., cannonball)
using the
% equations of motion, ignoring wind
resistance,
% and assuming it was fired on level
ground.
|
% User supplies: muzzle velocity,
angle of barrel
% inclination and the number of
"frames" over
% which a snapshot will be given.
|
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Email: kursusmatlab@gmail.com % This script is an example as part of the Matlab
% lecture in CS1E covering I/O.
|
% Author: Spike
% Date: 16/2/1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gravity = 9.81; % Acceleration due to
%
gravity.A constant.
|
KURSUS MATLAB ONLINE Skripsi, Tesis, DISERTASI 081219449060
Selasa, 13 Januari 2015
Managing Workspace in Matlab: load-KURSUS MATLAB ONLINE Skripsi, Tesis, DISERTASI 081219449060
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar