Page 839 - Mechatronics with Experiments
P. 839
®
®
MATLAB , SIMULINK , STATEFLOW, AND AUTO-CODE GENERATION 825
The fprintf() output can also be directed to a file, as follows,
x = 0:.1:1;
y = [x ; exp(x)];
fHandle = fopen(’exponential.txt’,’wt’);
fprintf(fHandle,’%6.2f %12.8f\n’,y);
fclose(fHandle);
®
Data can be input from different file formats that MATLAB ’s built-in functions
®
support. Below is a list of typical file formats supported by MATLAB built-in functions.
File Format Filename extensions MATLAB® Built-in Functions
———————————————————————————————————————————————————————————————————-
Text File ∗.dat, ∗.txt, ∗.mat y= load(’ .. ’)
save(’....’, y)
fopen()
fclose()
fscanf()
fprintf()
Excel File ∗.xls y=xlsread(’..’)
xlswrite(’..’,y)
Image File ∗.bmp, ∗.jpg, ∗,jpeg WL=imread(’wheel_loader.jpg’) ; % to read
image(WL); % to display
imwrite(WL,’MWL.jpg’) % to write
Audio File ∗.wav, ∗.au [Adata,Af]=wavread(’myAudio1.wav’) ; % to input
sound(Adata,Af) ; % to play it
wavwrite(Adata,Af, Ares,’myAudio2.wav’) % to
% write it
Video File ∗.avi getframe()
movie()
where Af is sampling frequency in Hz and Ares is the number of bits per sample (resolution
of sound data). In sound file of type “∗.wav,” the magnitude of the data should be between
−1.0 and 1.0 which represents the amplitude or the volume of the sound.
®
A useful MATLAB built-in function for user interface using monitor and keyboard
is the menu() function. The menu() function is used to display a window on the screen and
provides a number of choices, whichever one is selected by the user, the result is returned
to the variable on the assignment statement as an integer, indicating the choice. The general
format of the “menu()” function is as follows,
choice = menu(’menu_header_message’, ’item1 text’, ’item2
text’, ... )
and an example of usage is given below.
choice = menu(’Simulation choices:’, ’Case 1’, ’Case 2’,
’Case 3’)
switch choice
case 1
disp(’Case 1 is selected’);
case 2
disp(’Case 2 is selected’);
case 3
disp(’Case 3 is selected’);
end