How to use MathScripts in LabView : tutorial 15



In this tutorial, I have explained the working of MathScript in LabView. At the start an explanatory introduction of MathScript is provided to have some basic knowledge of the window. After that some of the basic commands and their uses are explained with examples. MathScript is a very extensive programming environment itself, it is impossible to explain its entire feature in one tutorial. However I tried to explain the basic features and gave you some knowledge of how to do self study on MathScript. At the end of the tutorial you are provided with an exercise to do it by yourself, and in the next tutorials I will assume that you have done those exercises and I will not explain the concept regarding them.



Introduction to mathScripts in labview

Textual math can be added to the LabView development environment with a compiler for the .m files, added by LabView MathScript Modules. Textual and graphical approaches can be blend together in MathScript. It may be used for algorithm development, control designs, also including data processing and analysis and signal processing tasks. Without extra code generation steps, you can easily deploy your .m code to real-time hardware.

It has many advantages over different programming languages i.e. you can save you valuable time with LabVIEW MathScript, which is generally compatible with almost all the .m file compilers i.e. MATLAB and Octave etc. You can reuse your already developed i.e. existing .m files in line with G code for analysis, processing or data presentation using this module of LabView.

Libraries related to engineering and scientific control like dials and controls are already present in the built-in LabVIEW GUI. You can always use the built-in LabView features to add user interaction and decision making to your .m files, and take advantages from LabViews’ MathScript.



You have all of the tools you need to deploy your custom .m files for deterministic execution with LabVIEW and MathScript, with a framework to both develop and test your .m files.

Explanation MathScripts with example 

  • In the search of your laptop or desktop bar write LabView, and don’t open a blank VI, as we have been doing so far, if you are interested in using MathScript. The window given in the figure below will appear, after you open LabView.MathScript in LabView

Figure 1: Main page

  • Click on the tools button in the top bar, a dropdown will appear, select Mathscript window form that menu as shown in the figure below,Opening Mathscript

Figure 2: Opening MathScript window

  • When you click on this mathscript window a window will appear with initializing written on top of it, as shown in the figure below,MathScript in LabView

Figure 3: Initializing a mathscript window

  • After the window has been initialized, mathscript will give you a help instruction. This help instruction is very important in mathscript. See the figure below,MathScript in LabView

Figure 4: Help instruction

  • When you type this help classes in the command window, as shown in the figure below,MathScript in LabView

Figure 5: Help classes

  • A window will appear, that will display all the help features that could be needed while writing a code in mathscript. See the figure below for reference,MathScript in LabView

Figure 6: Help window

  • You can get almost every help regarding the coding for this window, scroll down to see the features that this help window provides, and see the figure below,MathScript in LabView

Figure 7: Features of help window

  • When you open a mathscript window, it will shown a variables window, at the right portion of the window as shown in the figure below,MathScript in LabView

Figure 8: Variables portion

  • All variables once declared or used, either in command window or in the script will appear in this section, no matter either the variables used in the code are local or global.
  • The code written in mathscript cannot be accessed again as it is not saved nowhere. However we can write a code in mathscript that can be saved.
  • The script to write multiple lines of code and save it for future use is also available in mathscript window. The figure below shows a blank script in which you can write desired code.MathScript in LabView

Figure 9: Script

  • You can also see the history of all the commands you have typed so far after opening the mathscript by clicking on the history button next to script, as shown in the figure below,MathScript in LabView

Figure 10: History

  • We can also declare variables and add them in the command window, the variables and all off the answers will appear in the variable section as shown in the figure below,MathScript in LabView

Figure 11: Addition and variables

  • The variables’ window not only shows the name of the variables, but also their value and data type, as is obvious from the above figure.
  • If you place a semi colon (;) after a command written in the command window, the result of it will not appear in the Output window, as shown in the figure below,MathScript in LabView

Figure 12: Suppressed output

  • As you declare a simple variable in command window, you can also declare an array using square brackets,MathScript in LabView

Figure 13: Array declaration

  • We can also find the transpose of the array by using an apostrophe along with the name of the array, as shown in the figure below,MathScript in LabView

Figure 14: Array transpose

  • Mathscript follow the matrix multiplication rule, and we can’t multiply the matrix which don’t follow the multiplication rule of 2 matrices, i.e. a matrix in the above example cannot be multiplied by itself mathscript will give an errorMathScript in LabView

Figure 15: Matrix multiplication error

  • However multiplying the array with its transpose will satisfy the multiplication rule of matrices and hence the error will be compensated.MathScript in LabView

Figure 16: Satisfied matrix multiplication

  • The variable window however will not show the value of this matrix but only the size, and if you want to see its value you can double click on the size and the value will be displayed in the section below the variables section,MathScript in LabView

Figure 17: Matrix in variable window

  • You can also write multiple lines of code in the script, for instance, declare an array take its complement, multiply it with a scalar or add two arrays, and after doing all this run the code once, in spite of doing all this work statement wise. Lets’write a simple addition multiplication code in the script as shown below,MathScript in LabView

Figure 18: Script code

  • You can save the script for future use by pressing the button shown in the figure below,MathScript in LabView

Figure 19: Saving a script

  • A script can be run by pressing the green arrow above the script as shown in the figure below, or simply by pressing <Ctrl+R>MathScript in LabView

Figure 20: Running the script

  • The output of the script is shown in the figure below,MathScript in LabView

Figure 21: Output of script

  • You can also see the present working directory of the code by writing pwd in the command window, as shown in the figure below,MathScript in LabView

Figure 22: Present working directory.

  • MathScript is also used for plotting. An array can be used to plot a function, because plotting of a function requires the value of the function at multiple points. To generate multiple points, a mathscript command colon (:) is used as shown in the figure below,MathScript in LabView

Figure 23: Multiple point generation

  • The output of this function is an array, as shown in the figure below,MathScript in LabView

Figure 24: Multipoint output

  • Use a sin function to plot the function at t points as shown in the figure below,MathScript in LabView

Figure 25: Sin function generation

  • The output of this function is shown in the figure below,MathScript in LabView

Figure 26: Output of sin

The resolution of the sin function can be increased by decreasing the interval of the t function from 0.01 to 0.0001 or further less.

Exercise:

  • Plot all the trigonometric functions that are feasible for plotting.
  • Try to adjust the range of x and y axis
  • Label both the axes and give title to your graphs.

(Hint: Use help command for all the above tasks)

<< Previous tutorial                                Next tutorial>>



Add Comment