Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / form

A Matlab - Octave Form Generator

0.00/5 (No votes)
30 Aug 2021CPOL3 min read 5.6K   43  
The article is about Matlab - Octave scripts for easy create forms with the most useful controls (texts field, lists, radio buttons, check boxes, sliders, ...)
The article is a tutorial about a script that permits to generate forms; it has been developed in language Matlab with the aim of being compatible with Octave without any change.

Background

The use of the code, that has been developed in a common subset of Matlab and Octave, is intended for programmers that develop with these languages.

Introduction

The article is not a user manual (that is attached) but is meant to give an idea of ​​the capabilities of the product and how it frees the user from managing the geometry of the form.

Using the Code

The form builder is contained in formGen.m script, which contains the class formGen. Two other scripts fg_HandleButtons.m and fg_HandleEvents.m contain functions to deal with buttons and events.

In order to try the package, there is a script sandBox.m in the attached file that shows the capabilities by generating some forms.

The form is generated calling the fGen function of formGen class:

objForm=formGen; 
% here we can modify objects properties 
fGen(objForm, params, @handleAnswer) 
% or directly: 
formGen.fGen(formGen, params, @handleAnswer)

Where fGen parameters are:

  • the object itself
  • the list of fields form
  • the function that must be invoked in form closure

The Object Itself

This parameter is required by the object structure implemented in Matlab - Octave.

The List of Form Fields

The widgets (or controls) list is the basis for generating the form; every control is a set of attributes separated by comma: control type, name, label that must be exposed, length, default value and extra attribute(s). Controls are separated by semicolons.

In addition to the controls, we can have some others information (pseudo types) with different semantics.

The form generated can contain:

  • buttons: the Ok, Cancel and Reset buttons are automatically added to the form
  • texts: character texts possibly multi lined, numeric texts (integer, float and imaginary)
  • lists: a list can be associated to a text field for emulating a list with choice off the list itself
  • radio buttons
  • check box
  • sliders

The label is shown to the left of the control or, for buttons, is the caption.

The default value of numeric controls can be a random value inside a range of two numbers, for imaginary numbers:

  • One number: random values inside the circle with this radius and center (0,0)
  • Two numbers: first is the real part, second the imaginary part
  • Three numbers: random values inside the circle with radius first value and center (second and third value)

The content of extra field depends on the type of control, for instance the extra field of a slide can be the interval; for text can be a tool tip

formGen is not only a set of controls rough and ready but a mechanism for manage data: numeric fields are checked, buttons can be inserted for call a user function and, by some information (Pseudo types) inserted in the controls list, it is possible to manage some aesthetic appearance or interact with the user (the event pseudo type).

  • form pseudo type allows to configure the form background, position and modality
  • event attaches a function to a control
["FORM,,Numbers example,cyan,modal,,100,100;"...
            "N,Iteration,Iterations number,,97 107;"...
            "NS,Temperature,,,-50 50;"...
            "B,bSqrt,Sqrt,40,Sqrt,Imaginary,Square root;"...
            "I,Imaginary,Imaginary number,,3;"...
            "C,,Error comment,,,left,error;"];

Accessing Form Data

When the form is closed, the function (third parameter) receives a data in a structure accessible by the field name. Data are numeric for numeric fields or slide otherwise are string of characters.

Points of Interest

The language is born for mathematicals so it deals with string variables, it is not so easy like other languages; the GUI has a minimum support of event and no support of transparency but on the other hand the function guidata and the userdata parameters of controls (effectively an extended tag) facilitates the management and control of the data entered.

History

  • 30th August, 2021: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)