# Connexions

You are here: Home » Content » Freshman Engineering Problem Solving with MATLAB » A Very Brief Introduction to Programming with M-Files

• #### Programming with M-file Scripts

• A Very Brief Introduction to Programming with M-Files

### Recently Viewed

This feature requires Javascript to be enabled.

Inside Collection (Course):

Course by: Darryl Morrell. E-mail the author

# A Very Brief Introduction to Programming with M-Files

Module by: Darryl Morrell. E-mail the author

Summary: This module gives a very brief introduction to some of the concepts necessary to program with m-files.

You can use m-file scripts to automate computations. Almost anything typed at the command line can also be included in a m-file script. Lines in a m-file script are interpreted sequentially and the instructions are executed in turn. M-file scripts allow you to implement complex computations that cannot be readily achieved using commands at the command line. You can also create computational capabilities for other people to use.

There are some differences between MATLAB, MathScript, and Octave script files; these differences are typically not that significant. M-file scripts are text files and can be edited by any text editor. The script file must have an extension of ".m" and be in a directory that MATLAB knows about. M-file names should begin with a letter and only contain letters and numbers. Any other characters (space, dash, star, slash, etc.) will be interpreted as operations on variables and will cause errors. Also, M-file names should not be the same as variables in the workspace to differentiate between file names and variables.

## MATLAB and LABVIEW MathScript:

Both MATLAB and LABVIEW MathScript have built-in editors with features that make editing m-file scripts easier. In both envrionments, the editor is integrated with a debugger which makes finding and correcting errors in your scripts easier. More detailed information about using the MATLAB editor be found at Mathworks Matlab Tutorial-Creating Scripts with MATLAB Editor/Debugger. More detailed information about using the LABVIEW MathScript editor be found at National Instruments LabVIEW MathScript Tutorial-Inside LabVIEW MathScript Tutorial..

M-file scripts interact with the current executing environment. Variables set before the script is executed can affect what happens in the script. Variables set in the script remain after the script has finished execution.

Comments document your code and help other users (and yourself several months down the road) understand how you have implemented your program. Comments begin with the character %; any text in a line after the % is ignored by the script interpreter.

To correctly execute a script, the script file environment must know the directory in which the script resides.

## MATLAB:

To instruct the MATLAB environment where to search for the m-file script, you can set the current working directory or set the search path. More detailed information can be found at Mathworks Matlab Tutorial-Working with Files, Directories and Paths.

## LABVIEW MathScript:

To set the current working directory for LABVIEW MATHSCRIPT, use the menu File>MathScript Preferences in the MathScript interactive window. More detailed information can be found at National Instrument's LabVIEW MathScript Preferences Dialog Box.

## Content actions

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

#### Collection to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks

#### Module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks