[Tutorial] STM32F407 of DSP Chapter 5 Matlab ease of use of common programming statements

Download the full version of Guide: http://www.armbbs.cn/forum.php?mod=viewthread&tid=94547

Chapter 5 Matlab ease of use of common programming statements

This issue tutorial is to explain some of Matlab programming statements.

table of Contents

Chapter 5 Matlab ease of use of common programming statements

5.1 Important Beginners

5.2 Matlab control flow

5.2.1 Matlab condition control if, else, switch

5.2.2 Matlab loop control for, while, continue, break

5.2.3 Matlab vectorization

5.3 Matlab using the help function

5.4 summary


 

5.1 Important Beginners

  1.   This chapter before the holiday, be sure to learn a priority in Chapter 4.
  2.   Matlab programming statements similar to C, but more relaxed than C.

5.2 Matlab control flow

5.2.1 Matlab condition control if, else, switch

Let us illustrate the use of these three functions through three simple examples.

  •   If the use and else statements
a = randi(100, 1);

if a < 30
    disp('small')
elseif a < 80
    disp('medium')
else
    disp('large')
end

Command window output results are as follows:

 

  •   switch using statement
[dayNum, dayString] = weekday(date, 'long', 'en_US');

switch dayString
   case 'Monday'
      disp('Start of the work week')
   case 'Tuesday'
      disp('Day 2')
   case 'Wednesday'
      disp('Day 3')
   case 'Thursday'
      disp('Day 4')
   case 'Friday'
      disp('Last day of the work week')
   otherwise
      disp('Weekend!')
end

Command window output results are as follows:

 

In a similar way here described input C language function scanf and if else to achieve the above with a small feature:

yourNumber = input('Enter a number: ');

if yourNumber < 0
    disp('Negative')
elseif yourNumber > 0
    disp('Positive')
else
    disp('Zero')
end

After running the above code, we enter the number 22 in the command window, the output results are as follows:

 

5.2.2 Matlab loop control for, while, continue, break

Here we have to explain the use of these functions through a few simple examples.

  •   for use statement
for n = 3:32
   r(n) = rank(magic(n));
end

r

Command window output results are as follows:

 

  •   while using statement
a = 0; fa = -Inf;
b = 3; fb = Inf;
while b-a > eps*b
   x = (a+b)/2;
   fx = x^3-2*x-5;
   if sign(fx) == sign(fa)
      a = x; fa = fx;
   else
      b = x; fb = fx;
   end
end
x

Command window output results are as follows:

 

  •   continue to use statements
fid = fopen('magic.m','r');
count = 0;
while ~feof(fid)
    line = fgetl(fid);
    if isempty(line) || strncmp(line,'%',1) || ~ischar(line)
        continue
    end
    count = count + 1;
end
fprintf('%d lines\n',count);
fclose(fid);

Command output window:

 

  •   break using statement
a = 0; fa = -Inf;
b = 3; fb = Inf;
while b-a > eps*b
   x = (a+b)/2;
   fx = x^3-2*x-5;
   if fx == 0
      break
   elseif sign(fx) == sign(fa)
      a = x; fa = fx;
   else
      b = x; fb = fx;
   end
end
x

Command window output results are as follows:

 

5.2.3 Matlab vectorization

For matlab terms, in order to accelerate the execution speed of the algorithm can be implemented by vectorization algorithm, such as to achieve the following functions.

x = .01;
for k = 1:1001
   y(k) = log10(x);
   x = x + .01;
end

 

But we vectorization, will be more convenient and easy to implement.

x = 01: 01: 10; 
y = log10 (x);

 

But one thing we should pay special attention to, not what the program can speed up the execution of the vector, as the case may be.

5.3 Matlab using the help function

Something about getting started matlab's just so much you will basically have these basic enough, any problems encountered later to find information online. You can also view the help matlab help documentation itself. Click here to view, or direct access to the address:

https://ww2.mathworks.cn/help/

If you do not understand the function can be directly in the command window, type the help function can be combined with, for example, enter:

5.4 summary

Tutorial Matlab aspects just like to tell you so much, need that knowledge back when we re-specific supplement. Learn these basic operations can be a beginning. Always remember, Matlab is just a tool, we just treat it as a tool to use, no need to spend a lot of time to study, learn what entry after what can be.

 

He published 189 original articles · 87 won praise · views 60000 +

Guess you like

Origin blog.csdn.net/Simon223/article/details/105286068