Problem with writing a matrix to jxf File and reading it back to Jitter

Connector's icon

I am writing a c-external which should write different Values to a 2x2x3 Matrix. The Matrix should be stored in a .jxf file and should be able to read back in a normal Jitter Patch for further processing.

My brightness matrix is defined here:

 if (x->brightnessmatrix == NULL) {
     jit_matrix_info_default(&info);
     info.planecount = 1;
     info.dimcount = 3;
     info.dim[0] = x->cols; //set to 2
     info.dim[1] = x->rows; //set to 2
     info.dim[2] = x->frames; //set to 3
     info.type = _jit_sym_float64; //_jit_sym_float32
     x->brightnessmatrix = jit_object_new(_jit_sym_jit_matrix, &info);
 }

I am setting different brightnes Values inside a loop for each cell and every Frame:

for (i = 0; i < x->nrOfCells; i++) {
    yCellPos = i / x->cols; 
    xCellPos = i % x->cols; 
    
    brightness = 25. * i;
    post("i: %ld", i);
    post("x->currentframe: %ld", x->currentframe); //is set outside the loop
    post("brightness: %f", brightness);

   
    brightnessp = brightness_bp  //pointer to matrix
        + xCellPos * sizeof(float) //xCellPos times 
        + yCellPos * sizeof(float) * (&brightness_minfo)->dim[0]
        + x->currentframe * sizeof(float) * (&brightness_minfo)->dim[0] * (&brightness_minfo)->dim[1];
    *brightnessp = brightness;

}

When i Run the code above i get following output in max console:

c-external output

I am saving the brightness Matrix with:

t_jit_err testexternal_savematrices(t_testexternal* x, long ac, t_atom* av) {
    int t, h, z, e, i;
    char name[MAX_FILENAME_CHARS];
    t_filehandle fh;
    t_ptr_size position;
    long err;
    short pathId, exist, binflag;
    position = 0;
    binflag = 0;
    err = JIT_ERR_NONE;

    pathId = path_getdefault();

    for (i = 0; i < x->nrOfCells; i++) {
        t = i / 1000;
        h = (i - 1000 * t) / 100;
        z = (i - 100 * h - 1000 * t) / 10;
        e = (i - 10 * z - 100 * h - 1000 * t);

        sprintf(name, "cellmatrix%u%u%u%u.jxf", t, h, z, e);
        exist = locatefile(name, &pathId, &binflag);
        if (!exist)
        {
            path_deletefile(name, pathId);
            object_error((t_object*)x, "%s: overwriting file", name);
        }

        if (err = path_createsysfile(name, pathId, 'JIT!', &fh)) {
            error("could not create file a");
            goto out;
        }

        if (jit_bin_write_header(fh, position)) {
            error("could not write header");
            sysfile_close(fh);
            goto out;
        }
        if (jit_bin_write_matrix(fh, x->gridMatrices[i])) {
            error("could not write matrix");
            sysfile_close(fh);
            goto out;
        }
        sysfile_getpos(fh, &position);
        sysfile_seteof(fh, position);
        if (jit_bin_write_header(fh, position)) {
            error("jit.matrix: could not write header");
            sysfile_close(fh);
            goto out;
        }
        sysfile_close(fh);
    }

    sprintf(name, "brightness.jxf");
    exist = locatefile(name, &pathId, &binflag);
    if (!exist)
    {
        path_deletefile(name, pathId);
        object_error((t_object*)x, "%s: overwriting file", name);
    }

    if (err = path_createsysfile(name, pathId, 'JIT!', &fh)) {
        error("could not create file b");
        goto out;
    }

    if (jit_bin_write_header(fh, position)) {
        error("could not write header");
        sysfile_close(fh);
        goto out;
    }
    if (jit_bin_write_matrix(fh, x->brightnessmatrix)) {
        error("could not write matrix");
        sysfile_close(fh);
        goto out;
    }
    sysfile_getpos(fh, &position);
    sysfile_seteof(fh, position);
    if (jit_bin_write_header(fh, position)) {
        error("jit.matrix: could not write header");
        sysfile_close(fh);
        goto out;
    }
    sysfile_close(fh);

out:
    return err;
}

When i use following Patch to load the brightness.jxf back to Max Application i get only 0. values in the matrix:

ReadingMatrix.maxpat
Max Patch
wrong output Values

So is there another way to read the values in a 2x2x3 Matrix?

Or have i made a mistake while storing the values into the Matrix in my external?