Advanced Octave A04 — Empty values, nulls and missing data

by Giux GX-TXT, octave-en, scripts-en 3 min read

Advanced GNU Octave with GX-TXT · Module 01

A04 — Valori vuoti, null e dati mancanti

An empty container, a null value and a missing measure can express different concepts. With Octave and Bridge it is essential to know both the representation in language and the meaning attributed by each API.

Objectives and prerequisites

After the introductory course and the A03 lesson we know matrices, cells and structures. Now we compare three containers and use an empty argument in a call where the contract provides it. The test uses GX-TXT dev112-P011-bis and GNU Octave 9.4.0.

Three different forms

Import valori_vuoti.m, record it in the Session and start it with the Runner.

% Advanced Octave course A04: empty values and API-specific conventions.
% SPDX-License-Identifier: GPL-3.0-or-later
gxtxt_api_require({'NUMERIC_API_v1@1:math.numeric+complex_linear_algebra'});
empty_array = [];
empty_cell = {};
empty_struct = struct();
fprintf('Empty numeric array: class=%s, numel=%d\n', class(empty_array), numel(empty_array));
fprintf('Empty cell: class=%s, numel=%d\n', class(empty_cell), numel(empty_cell));
fprintf('Empty struct: class=%s, fields=%d\n', class(empty_struct), numel(fieldnames(empty_struct)));
A = struct('real', [2, 0; 0, 4], 'imaginary', zeros(2));
result = gxtxt_api_call('NUMERIC_API_v1', 'linear_matrix', {'inspect', A, []});
fprintf('Inspect matrix: rows=%d, cols=%d, rank=%d\n', result.rows, result.cols, result.rank);

[] creates an empty numerical array, {} an empty cell array, and struct() a fieldless structure. The three objects do not have the same class: the fact that they are empty does not make them replaceable in API requests.

The output shows the double classes, cell and struct respectively; the inspection returns two rows, two columns and two ranks. For the inspect action of linear_matrix, the contract allows you to pass [] as the second operating unused. This is a documented use for that call, not a universal rule for all optional parameters.

When it comes null

The Bridge documentation specifies that an explicit null value received from the transport is projected in Octave as []. Since Octave does not have a distinct JSON null climber in this case, the projection can lose the information that would allow to distinguish it from an empty numerical array. If an API provides an authoritative result_json for this purpose, consult it according to your contract. Sending ordinary [] from the client instead represents an empty numerical array: it does not automatically build a null value.

None of these forms alone says that a measure is missing or equal to zero. For experimental data use the schema and validity conventions declared by the Dataset or by the operation; do not replace the absent data with zero without an explicit scientific choice.

Exercise

  1. What class does {} have? What about []?
  2. Why is the [] argument in linear_matrix/inspect allowed in the example?
  3. Is it correct to deduct from a [] result that a measure is zero?

Worked solution

  1. cell and double respectively.
  2. Because the contract of the action used provides for a second empty operation when it is not needed.
  3. No: it is necessary to distinguish representation, contract and scientific meaning of the data.

Download the Module 01 scripts (ZIP)

#GNU Octave #GX-TXT #GX-TXT public APIs

Other steps of this project

Leave a Reply

Your email address will not be published. Required fields are marked *