[To] http://blog.21ic.com/user1/3128/archives/2010/71193.html
MATLAB is a common tool. Quick learning and mastering of MATLAB makes sense for efficient completion of work. This article describes how to learn and use MATLAB based on personal experience. These methods have been proven by several people.
- The basic syntax for learning Matlab is skipped. If you are not sure, you can try it with some simple numbers below the command. If you are used to C, pay attention to matrix calculation. What is matrix calculation? That is, try to replace the loop, especially the loop with if, with a matrix. For example, if you set all the data in the array greater than 1000 to 1000, and the other data remains unchanged, you can write x (x> 100) = 100; if the for loop is used and if is used, the efficiency is 1/4. Sometimes it seems that you have to use the for solution. For example, if you want to change an array of 1x6 to 2 rows and 3 columns, you can look for the library function. In this example, you can use reshape to solve the problem. If you must use a loop and assign values to several subscripts of an array X, you must first define the array X. For example, you can use X = zeros ). Otherwise, an array with an indefinite length is declared, and the efficiency is very low. This is the cause of the vast majority of program efficiency losses, of course, there is a way to check. The method is to use the profiler tool provided by Matlab. In tools> open profiler, the toolbar also has icons. Write the name of the function or M file you want to run on this tool, and the program starts to run. After the operation is completed, the execution time of each statement is counted, you can find the crux of the problem. If it is an array assignment operation in the loop body, it is generally abnormal. You should check whether the initialization error has been made. Sometimes some functions take up a lot of time. You can find a way to replace them. If you don't have them, go inside the function and try again. Sometimes there is room for optimization. The final result is often that most of the time has been clearly consumed, such as calling underlying C functions, convolution operations, matrix operations, and so on.
- Clear, CLC, and figure should be kept in mind in common MATLAB functions. In addition, sometimes you need to pay attention to the second return value of some library functions, which is often what you want. For example, the maximum value of the first value of the Max function, and the second value is the badge of the array, you can directly use [value index] = max (X) to get it at the same time. If you lose the second number and use find to find it, it will be exhausted.
- Learning the Toolbox function of MATLAB and Its Usage do not need to look at the books, it is enough help. Open Help-> product help. Many Toolboxes are listed on the left. Each toolbox has several subitems. Getting Started has the most authoritative example. If you need to use it for a long time, you should read the User's Guide once. There is not much content. Generally, one or two days is enough. There are a lot of sample code in it, and some useful parts must be tried by yourself. In addition, it has examples for typical applications. Although these algorithms are somewhat "old" for cutting-edge fields, their refinement and efficiency are incomparable to other teaching materials. It will show you the minimum lab skills and topics discussed by experts in this field, and even can be used as lab textbooks for majors you have learned. If you are interested in the specific functions and their implementations in the above process, you can find them in S, where each function is explained in detail. If you are very interested in the implementation of this function, you can directly write "Edit function name" in the command to see the source code of its implementation. Some graphic tools, such as fdatool, imtool, and bertool. A function may be nested with a deeper function, so please call it with the data you are most familiar with, and check it step by step through debugging + one-step tracking, since data is familiar to you, you can quickly handle the internal work mode.
There are two exceptions: one is the object type enabled by the new function of Matlab, which is encapsulated in its source code. It may not be able to see anything through edit. How can we find its source code? There are two ways: one is single-step tracking. If it still cannot be solved, you can use the profiler tool mentioned above, which will write the called functions in one column, it is very likely that a function at the top can not go in. You can set a breakpoint on the function under it to see how the data is running, and then follow up step by step, basically, there will be no points that cannot be followed by a single step.
The other exception is when you encounter buildin functions or c Functions of Matlab, so there is no way, because these two functions do not have source code.
The functions of the MATLAB library have a unified structure. The first part is the help information, which can be seen by help, followed by the variable check, and finally the implementation process. Because many functions are universal, the implementation details are not optimal, and too many checks are made on the parameters. If you need better performance, you can extract the key statements, or expressed in a pure matrix operation, it can often speed up. In addition, you can also write your comments on the frontend side so that you can use help to find them.
- There are several shortcut keys in the source code editor that are very useful. We recommend that you use Ctrl + R to annotate the selected code, CTRL + T to cancel the annotation, F5, F10, F11, and so on for debugging, terminate the running program Ctrl + C. In addition, if you encounter a function that you want to view when reading the source code in the source code editor, right-click the function and select Open selection. The code saved by MATLAB can also be undone, or even removed to the initial state when Matlab is enabled. Therefore, it should be stored frequently to prevent document loss. The source code can be separated by two percentage signs "%", or even only part of the code is executed, which is very helpful for long source code.
If you want to call a function or variable in command, you can obtain the matching prompt by typing the first few letters and then pressing the tab key. To view variables, especially 2D arrays, you can use workspace to find them and double-click them to open the variable editor. This editor can copy and paste each other with Excel, which is very convenient. In addition, most of the fonts and colors of various forms can be changed. In preferences, there are fonts and other projects. You can take a look. Finally, it is worth mentioning that the image drawn by the plot is a vector image. If there are many data points, copying the image to the word may display a card or it is very slow. You can copy the image to the drawing first, then paste it to the word, which is the bitmap.
- If you encounter a large amount of computing, we recommend that you do not directly plot or print the results after the computation, but use an independent program architecture for the calculation and result display. Computation is a program. The computation result is stored as a mat file using the Save statement, and then read the file using another program using the load statement. This prevents loss and tampering of results. In addition, the progress of computing can be displayed in command in real time to display the intuition and convenience of debugging. For example:
Type = {'aach ''bsch ''' tch/4.8 _ 1 '};
I = 3; A = 1;
Fprintf ('calculating Logical Channel % s using % d antennas \ n', cell2mat (Type (I), );
The
Calculating Logical Channel tch/4.8 _ 1 using 1 Antennas
If a log file is created, it is better. First, open a file and write the timestamp clock. It is an array that represents the year, month, day, hour, minute, and second.
Logfid=fopen('log7-26.txt ', 'a + ');
Fprintf (logfid, 'time ');
Fprintf (logfid, '% d.', clock );
Fprintf (logfid, '\ n ');
Fprintf is still used for printing, but the logfid handle must be added.
Fprintf (logfid, 'calculating Logical Channel % s using % d antennas \ n', cell2mat (Type (I), );
Finally, fclose (logfid) is used to close the log file. Fortunately, if this statement is not executed due to the termination operation, most log results are saved.
If you want to automatically shut down the computer after the computation is completed, you can add
! Shutdown-S
An exclamation point indicates that an external command is executed, followed by a Windows Default Shutdown program. You can experiment in the run dialog box. If you need to stop shutdown, run shutdown-A again.
- There are a lot of MATLAB functions, and new functions emerge one after another. It is best to use a new version. MATLAB is not always correct. Many details are biased, and I have met it several times, the most serious one is that the results of the gaussfir function are all incorrect. Later, we found that the higher version has been corrected, so we should try to use a higher version. Sometimes, as the version is updated, it puts forward new requirements for your code, for example, replacing J with 1i to represent the plural, or using strcmp instead ~ = Compares strings and so on. It will be reminded by the Red Line. You can correct it according to its prompt.
7. I think it is intuitive to check whether or not to use SIMULINK, but the disadvantage is that the efficiency is not high (mainly for communication physical layer simulation), especially Monte Carlo simulation, which requires a large amount of data. At that time, I tried it. The senior student used to do it in the chain and ran for 50 thousand o'clock. I wrote the code myself and ran to 0.5 million o'clock, which was much faster than him, later, the internal implementation of extraction functions and matrix optimization methods were greatly improved. Sometimes we find that there are too many loops and judgments. We can only use C for them. This is the choice of many library functions of Matlab, so if you need higher efficiency, you can also do this. Although MATLAB-C joint programming is conducive to improving the operational efficiency (or even good use of multi-core), but it is difficult to debug C code, in the preparation of the buffer overflow and other issues, it causes the entire MATLAB to crash and restart. Therefore, it is best to debug it in VC and connect it with the interface. Of course, the C code is not a single step during debugging, but printf can still be used. In addition, the MATLAB interface creation tool is very easy to use, and the preparation is very good. It is recommended that you take some time to learn and be able to manipulate the button, reading the check box and text values is basically enough. It will make your result report very intuitive.