IV-4Macros. Chapter IV-4

Similar documents
II-7Numeric and String Variables

IV-8Debugging. Chapter IV-8

IV-3User-Defined Functions

IV-6Interacting with the User

II-3Experiments, Files and Folders

III-13Procedure Windows

CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

A Tutorial on using Code::Blocks with Catalina 3.0.3

II-4Windows. Chapter II-4

Total Test Questions: 43 Levels: Grades Units of Credit:.50

Type Checking and Type Equality

An Introduction to Python (TEJ3M & TEJ4M)

III-1Notebooks. Chapter III-1

Computer Programming IA

Getting Started (1.8.7) 9/2/2009

Pace University. Fundamental Concepts of CS121 1

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

Chapter 11 Running the Model

Short Notes of CS201

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

2 Getting Started. Getting Started (v1.8.6) 3/5/2007

Scheme of work Cambridge International AS & A Level Computing (9691)

CS201 - Introduction to Programming Glossary By

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

QUIZ. What is wrong with this code that uses default arguments?

Creating Word Outlines from Compendium on a Mac

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Perl Basics. Structure, Style, and Documentation

MATLAB provides several built-in statements that allow for conditional behavior if/elseif/else switch menu

Copyright. Trademarks Attachmate Corporation. All rights reserved. USA Patents Pending. WRQ ReflectionVisual Basic User Guide

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

Chapter 1 Getting Started

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

UNIT-IV: MACRO PROCESSOR

Have examined process Creating program Have developed program Written in C Source code

Volume II, Section 5 Table of Contents

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

EXCEPTION HANDLING. Summer 2018

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

S206E Lecture 19, 5/24/2016, Python an overview

II-8Data Folders. Chapter II-8

Lecture Outline. COMP-421 Compiler Design. What is Lex? Lex Specification. ! Lexical Analyzer Lex. ! Lex Examples. Presented by Dr Ioanna Dionysiou

A Fast Review of C Essentials Part I

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

ASYNCHRONOUS PROGRAMMING IN C# 5 WITHOUT USE OF MULTIPLE THREADS

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

Java Program Structure and Eclipse. Overview. Eclipse Projects and Project Structure. COMP 210: Object-Oriented Programming Lecture Notes 1

Sri Vidya College of Engineering & Technology

Windows

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Advanced Financial Modeling Macros. EduPristine

UNIT - 5 EDITORS AND DEBUGGING SYSTEMS

Usability I: Principles and Guidelines

Algorithmic "imperative" language

Hands-On Perl Scripting and CGI Programming

Makefile Tutorial. Eric S. Missimer. December 6, 2013

Installing and getting started with Xcode for Mac OS.

G Programming Languages - Fall 2012

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

A Tutorial for ECE 175

Bindel, Fall 2011 Applications of Parallel Computers (CS 5220) Tuning on a single core

III-15Platform-Related Issues

COMMON WINDOWS 10 QUESTIONS & ANSWERS

ECE 202 LAB 3 ADVANCED MATLAB

Guidelines for Coding of SAS Programs Thomas J. Winn, Jr. Texas State Auditor s Office

Libgdb. Version 0.3 Oct Thomas Lord

(Refer Slide Time 01:41 min)

Implementing Interfaces. Marwan Burelle. July 20, 2012

7 The Integrated Debugger

COMP 388/441 HCI: 07 - Menu Selection, Forms, and Dialog Boxes Menu Selection, Forms, and Dialog Boxes

TABLE OF CONTENTS 2 CHAPTER 1 3 CHAPTER 2 4 CHAPTER 3 5 CHAPTER 4. Algorithm Design & Problem Solving. Data Representation.

With IBM BPM 8.5.5, the features needed to express both BPM solutions and case management oriented solutions comes together in one offering.

PHP Personal Home Page PHP: Hypertext Preprocessor (Lecture 35-37)

Version 9 of the Icon Compiler. Ralph E. Griswold Department of Computer Science, The University of Arizona

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

d2vbaref.doc Page 1 of 22 05/11/02 14:21

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

III-14Controls and Control Panels

Laboratory Assignment #4 Debugging in Eclipse CDT 1

SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing

do fifty two: Language Reference Manual

CSPro Language Reference

Sprite an animation manipulation language Language Reference Manual

A Guided Tour of Doc-To-Help

Exceptions. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 15

SPARK-PL: Introduction

Computing and compilers

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

Type Checking in COOL (II) Lecture 10

Design by Contract in Eiffel

COPYRIGHTED MATERIAL. Part I: Getting Started. Chapter 1: IDE. Chapter 2: Controls in General. Chapter 3: Program and Module Structure

J.1 Pseudo-Variable

Review&Preview 1/23/15, 4:08:07 PM 1. 3rd edition - standardized, and standard library allows programmer to start from a higher level

Lesson 1. Introduction to Programming OBJECTIVES

Full file at

UNIT V SYSTEM SOFTWARE TOOLS

Transcription:

Chapter IV-4 IV-4Macros Overview... 108 Comparing Macros and Functions... 108 Macro Syntax... 110 The Defining Keyword... 110 The Procedure Name... 110 The Procedure Subtype... 110 The Parameter List and Parameter Declarations... 111 Local Variable Declarations... 111 Body Code... 111 Conditional Statements in Macros... 112 Loops in Macros... 112 Return Statement in Macros... 112 Invoking Macros... 112 Using $ in Macros... 113 Waves as Parameters in Macros... 113 The Missing Parameter Dialog... 113 Macro Errors... 114 The Silent Option... 114 The Slow Option... 114 Accessing Variables Used by Igor Operations... 115 Updates During Macro Execution... 115 Aborting Macros... 115 Converting Macros to Functions... 116

Overview When we first created Igor, some time in the last millennium, it supported automation through macros. The idea of the macro was to allow users to collect commands into conveniently callable routines. Igor interpreted and executed each command in a macro as if it were entered in the command line. WaveMetrics soon realized the need for a faster, more robust technology that would support full-blown programming. This led to the addition of user-defined functions. Because functions are compiled, they execute much more quickly. Also, compilation allows Igor to catch syntactic errors sooner. Functions have a richer set of flow control capabilities and support many other programming refinements. Over time, the role of macros has diminished in favor of functions. With rare exceptions, new programming should be done using user-defined functions. Macros are still supported and there are still a few uses in which they are preferred. When you close a graph, table, page layout, Gizmo plot, or control panel, Igor offers to automatically create a window recreation macro which you can later run to recreate the window. You can also ask Igor to automatically create a window style macro using the Window Control dialog. The vast majority of programming, however, should be done using functions. The syntax and behavior of macros are similar to the syntax and behavior of functions, but the differences can be a source of confusion for someone first learning Igor programming. If you are just starting, you can safely defer reading the rest of this chapter until you need to know more about macros, if that time ever comes. Comparing Macros and Functions Like functions, macros are created by entering text in procedure windows. Each macro has a name, a parameter list, parameter declarations, and a body. Unlike functions, a macro has no return value. Macros and functions use similar syntax. Here is an example of each. To follow along, open the Procedure window (Windows menu) and type in the macro and function definitions. Macro MacSayHello(name) String name Print "Hello "+name Function FuncSayHello(name) String name Print "Hello "+name w click in the command window to bring it to the front. If you execute the following on the command line MacSayHello("John"); FuncSayHello("Sue") you will see the following output printed in the history area: Hello John Hello Sue This example may lead you to believe that macros and functions are nearly identical. In fact, there are a lot of differences. The most important differences are: Macros automatically appear in the Macros menu. Functions must be explicitly added to a menu, if desired, using a menu definition. Most errors in functions are detected when procedures are compiled. Most errors in macros are detected when the macro is executed. IV-108

Functions run a lot faster than macros. Functions support wave parameters, for loops, switches, structures, multithreading, and many other features not available in macros. Functions have a richer syntax. If you look in the Macros menu, you will see MacSayHello but not FuncSayHello. If you execute FuncSayHello() on the command line you will see an error dialog. This is because you must supply a parameter. You can execute, for example: FuncSayHello("Sam") On the other hand, if you run MacSayHello from the Macros menu or if you execute MacSayHello() on the command line, you will see a dialog that you use to enter the name before continuing: This is called the missing parameter dialog. It is described under The Missing Parameter Dialog on page IV-113. Functions can display a similar dialog, called a simple input dialog, with a bit of additional programming. w try this: In both procedures, change Print to xprint. Then click in the command window. You will see an error dialog complaining about the xprint in the function: Click the Edit Procedure button and change xprint back to Print in the function but not in the macro. Then click in the command window. tice that no error was reported once you fixed the error in the function. This is because only functions are compiled and thus only functions have their syntax completely checked at compile time. Macros are interpreted and most errors are found only when the line in the procedure window in which they occur is executed. To see this, run the macro by executing MacSayHello("Sam") on the command line. Igor displays an error dialog: IV-109

tice the outline around the line containing the error. This outline means you can edit the erroneous command. If you change xprint to Print in this dialog, the Retry button becomes enabled. If you click Retry, Igor continues execution of the macro. When the macro finishes, take a look at the Procedure window. You will notice that the correction you made in the dialog was put in the procedure window and your broken macro is now fixed. Macro Syntax Here is the basic syntax for macros. <Defining keyword> <Name> ( <Input parameter list> ) [:<Subtype>] <Input parameter declarations> <Local variable declarations> <Body code> The Defining Keyword <Defining keyword> is one of the following: Defining Keyword Creates Macro In Window Windows menu Macro Macros menu Proc The Window keyword is used by Igor when it automatically creates a window recreation macro. Except in rare cases, you will not write window recreation macros but instead will let Igor create them automatically. The Procedure Name The names of macros must follow the standard Igor naming conventions. Names can consist of up to 31 characters. The first character must be alphabetic while the remaining characters can include alphabetic and numeric characters and the underscore character. Names must not conflict with the names of other Igor objects, functions or operations. Names in Igor are case insensitive. The Procedure Subtype You can identify procedures designed for specific purposes by using a subtype. Here is an example: Proc ButtonProc(ctrlName) : ButtonControl String ctrlname Beep Here, : ButtonControl identifies a macro intended to be called when a user-defined button control is clicked. Because of the subtype, this macro is added to the menu of procedures that appears in the Button IV-110

Control dialog. When Igor automatically generates a procedure it generates the appropriate subtype. See Procedure Subtypes on page IV-193 for details. The Parameter List and Parameter Declarations The parameter list specifies the name for each input parameter. Macros have a limit of 10 parameters. The parameter declaration must declare the type of each parameter using the keywords Variable or String. If a parameter is a complex number, it must be declared Variable/C. te: There should be no blank lines or other commands until after all the input parameters are defined. There should be one blank line after the parameter declarations, before the rest of the procedure. Igor will report errors if these conditions are not met. Variable and string parameters in macros are always passed to a subroutine by value. When macros are invoked with some or all of their input parameters missing, Igor displays a missing parameter dialog to allow the user to enter those parameters. In the past this has been a reason to use macros. However, as of Igor Pro 4, functions can present a similar dialog to fetch input from the user, as explained under The Simple Input Dialog on page IV-132. Local Variable Declarations The input parameter declarations are followed by the local variable declarations if the macro uses local variables. Local variables exist only during the execution of the macro. They can be numeric or string and are declared using the Variable or String keywords. They can optionally be initialized. Here is an example: Macro Example(p1) Variable p1 // Here are the local variables Variable v1, v2 Variable v3=0 Variable/C cv1=cmplx(0,0) String s1="test", s2="test2" <Body code> If you do not supply explicit initialization, Igor automatically initializes local numeric variables with the value zero and local string variables with the value "". The name of a local variable is allowed to conflict with other names in Igor although they must be unique within the macro. Clearly if you create a local variable named sin then you will be unable to use Igor s built-in sin function within the macro. You can declare a local variable in any part of a macro with one exception. If you place a variable declaration inside a loop in a macro then the declaration will be executed multiple times and Igor will generate an error since local variable names must be unique. Body Code The local variable declarations are followed by the body code. This table shows what can appear in body code of a macro. What Allowed in Macros? Comments Assignment statements Yes Includes wave, variable and string assignments. Built-in operations Yes External operations Yes External functions Yes IV-111

What Allowed in Macros? Comments Calls to user functions Yes Calls to macros Yes if-else-endif Yes if-elseif-endif switch-case-endswitch strswitch-case-endswitch try-catch-endtry structures do-while Yes for-endfor Comments Yes Comments start with //. break Yes Used in loop statements. continue default return Yes, but with no return value. Structures Conditional Statements in Macros The conditional if-else-endif statement is allowed in macros. It works the same as in functions. See If-Else- if on page IV-37. Loops in Macros The do-while loop is supported in macros. It works the same as in functions. See Do-While Loop on page IV-42. Return Statement in Macros The return keyword immediately stops executing the current macro. If it was called by another macro, control returns to the calling macro. A macro has no return value so return is used just to prematurely quit the macro. Most macros will have no return statement. Invoking Macros There are several ways to invoke a macro: From the command line From the Macros, Windows or user-defined menus From another macro From a button or other user control The menu in which a macro appears, if any, is determined by the macro s type and subtype. IV-112

This table shows how a macro s type determines the menu that Igor puts it in. Macro Type Defining Keyword Menu Macro Macro Macros menu Window Macro Window Windows menu Proc Proc Chapter IV-4 Macros If a macro has a subtype, it may appear in a different menu. This is described under Procedure Subtypes on page IV-193. You can put macros in other menus as described in Chapter IV-5, User-Defined Menus. You can not directly invoke a macro from a user function. You can invoke it indirectly, using the Execute operation (see page V-176). Using $ in Macros As shown in the following example, the $ operator can create references to global numeric and string variables as well as to waves. Macro MacroTest(vStr, sstr, wstr) String vstr, sstr, wstr $vstr += 1 $sstr += "Hello" $wstr += 1 Variable/G gvar = 0; String/G gstr = ""; Make/O/N=5 gwave = p MacroTest("gVar", "gstr", "gwave") See String Substitution Using $ on page IV-17 for additional examples using $. Waves as Parameters in Macros The only way to pass a wave to a macro is to pass the name of the wave in a string parameter. You then use the $ operator to convert the string into a wave reference. For example: Macro PrintWaveStdDev(w) String w WaveStats/Q $w Print V_sdev Make/O/N=100 test=gnoise(1) Print NamedWaveStdDev("test") The Missing Parameter Dialog When a macro that is declared to take a set of input parameters is executed with some or all of the parameters missing, it displays a dialog in which the user can enter the missing values. For example: Macro MacCalcDiag(x,y) Variable x=10 Prompt x, "Enter X component: " // Set prompt for y param Variable y=20 Prompt y, "Enter Y component: " // Set prompt for x param Print "diagonal=",sqrt(x^2+y^2) IV-113

If invoked from the command line or from another macro with parameters missing, like this: MacCalcDiag() Igor displays the Missing Parameter dialog in which the parameter values can be specified. The Prompt statements are optional. If they are omitted, the variable name is used as the prompt text. There must be a blank line after the set of input parameter and prompt declarations and there must not be any blank lines within the set. The missing parameter dialog supports the creation of pop-up menus as described under Pop-Up Menus in Simple Dialogs on page IV-133. One difference is that in a missing parameter dialog, the menu item list can be continued using as many lines as you need. For example: Prompt color, "Select Color", popup "red;green;blue;" "yellow;purple" Macro Errors Igor can find errors in macros at two times: When it scans the macros When it executes the macros After you modify procedure text, scanning occurs when you activate a non-procedure window, click the Compile button or choose Compile from the Macros menu. At this point, Igor is just looking for the names of procedures. The only errors that it detects are name conflicts and ill-formed names. If it finds such an error, it displays a dialog that you use to fix it. Igor detects other errors when the macro executes. Execution errors may be recoverable or non-recoverable. If a recoverable error occurs, Igor puts up a dialog in which you can edit the erroneous line. You can fix the error and retry or quit macro execution. If the error is non-recoverable, you get a similar dialog except that you can t fix the error and retry. This happens with errors in the parameter declarations and errors related to if-else-endif and do-while structures. The Silent Option rmally Igor displays each line of a macro in the command line as it executes the line. This gives you some idea of what is going on. However it also slows macro execution down considerably. You can prevent Igor from showing macro lines as they are executed by using the Silent 1 command. You can use Silent 1 from the command line. It is more common to use it from within a macro. The effect of the Silent command ends at the end of the macro in which it occurs. Many macros contain the following line: Silent 1; PauseUpdate The Slow Option You can observe the lines in a macro as they execute in the command line. However, for debugging purposes, they often whiz by too quickly. The Slow operation slows the lines down. It takes a parameter which controls how much the lines are slowed down. Typically, you would execute something like Slow 10 from the command line and then Slow 0 when you are finished debugging. You can also use the Slow operation from within a macro. You must explicitly invoke Slow 0 to revert to normal behavior. It does not automatically revert at the end of the macro from which it was invoked. We never use this feature. Instead, we generally use print statements for debugging or we use the Igor symbolic debugger, described in Chapter IV-8, Debugging. IV-114

Accessing Variables Used by Igor Operations Chapter IV-4 Macros A number of Igor s operations return results via variables. For example, the WaveStats operation creates a number of variables with names such as V_avg, V_sigma, etc. When you invoke these operations from the command line, they create global variables in the current data folder. When you invoke them from a user-defined function, they create local variables. When you invoke them from a macro, they create local variables unless a global variable already exists. If both a global variable and a local variable exist, Igor uses the local variable. In addition to creating variables, a few operations, such as CurveFit and FuncFit, check for the existence of specific variables to provide optional behavior. The operations look first for a local variable with a specific name. If the local variable is not found, they then look for a global variable. Updates During Macro Execution An update is an action that Igor performs. It consists of: Reexecuting assignments for dependent objects whose antecedents have changed (see Chapter IV-9, Dependencies); Redrawing graphs and tables which display waves that have changed; Redrawing page layouts containing graphs, tables, or annotations that have changed; Redrawing windows that have been uncovered. When no procedure is executing, Igor continually checks whether an update is needed and does an update if necessary. During macro execution, Igor checks if an update is needed after executing each line. You can suspend checking using the PauseUpdate operation. This is useful when you want an update to occur when a macro finishes but not during the course of the macro s execution. PauseUpdate has effect only inside a macro. Here is how it is used. Window Graph0() : Graph PauseUpdate; Silent 1 Display /W=(5,42,400,250) w0,w1,w2 ModifyGraph gfont="helvetica" ModifyGraph rgb(w0)=(0,0,0),rgb(w1)=(0,65535,0),rgb(w2)=(0,0,0) <more modifies here...> Without the PauseUpdate, Igor would do an update after each modify operation. This would take a long time. At the end of the macro, Igor automatically reverts the state of update-checking to what it was when this macro was invoked. You can use the ResumeUpdate operation if you want to resume updates before the macro ends or you can call DoUpdate to force an update to occur at a particular point in the program flow. Such explicit updating is rarely needed. Aborting Macros There are two ways to prematurely stop macro execution: a user abort or a programmed abort. Both stop execution of all macros, no matter how deeply nested. You can abort macro execution by pressing the User Abort Key Combinations or by clicking the Abort button in the status bar. You may need to press the keys down for a while because Igor looks at the keyboard periodically and if you don t press the keys long enough, Igor will not see them. IV-115

A user abort does not directly return. Instead it sets a flag that stops loops from looping and then returns using the normal calling chain. For this reason some code will still be executed after an abort but execution should stop quickly. This behavior releases any temporary memory allocations made during execution. A programmed abort occurs when the Abort operation is executed. Here is an example: if (numcells > 10) Abort "Too many cells! Quitting." endif // code here doesn't execute if numcells > 10 Converting Macros to Functions If you have old Igor procedures written as macros, as you have occasion to revisit them, you can consider converting them to functions. In most cases, this is a good idea. An exception is if the macros are so complex that there would be a substantial risk of introducing bugs. In this case, it is better to leave things as they are. If you decide to do the conversion, here is a checklist that you can use in the process. 1. Back up the old version of the procedures. 2. Change the defining keyword from Macro or Proc to Function. 3. If the macro contained Prompt statements, then it was used to generate a missing parameter dialog. Change it to generate a simple input dialog as follows: a. Remove the parameters from the parameter list. The old parameter declarations now become local variable declarations. b. Make sure that the local variable for each prompt statement is initialized to some value. c. Add a DoPrompt statement after all of the Prompt statements. d. Add a test on V_Flag after the DoPrompt statement to see if the user canceled. 4. Look for statements that access global variables or strings and create NVAR and SVAR references for them. 5. Look for any waves used in assignment statements and create WAVE references for them. 6. Compile procedures. If you get an error, fix it and repeat step 6. IV-116