[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: dumping scalar structure to matrix fails if empty rows exist

From: Olaf Till
Subject: Re: dumping scalar structure to matrix fails if empty rows exist
Date: Sun, 18 Oct 2015 20:21:51 +0200
User-agent: Mutt/1.5.23 (2014-03-12)

On Fri, Oct 16, 2015 at 08:28:42AM -0700, c031917 wrote:
> Hi,
> quite new to Octave, I don’t find a solution despite a lot of try and search
> :
> I want to write various scalar structures out to CSV Files, resembling
> telemetry data with spurious signal drop outs. I can’t influence the data
> sent. The code below works as long as all elements are filled.
> But if there are columns which do have column headers with empty values
> related to them, the assignment of this empty row to the prepared target
> matrix deletes the respective row,

you mean column, not row ...

> what leads to an  “A(I,J,...) = X: dimensions mismatch” error.
> Can I somehow prevent Octave from deleting that rows ? Or do I need a sort
> of “arrayfun” Fu to catch those [] cases ? I tried to understand that, but
> it is beyond my current level.. 
> The resulting CSV File should have the respective column header and empty
> cells (e.g. ;;) below.
> Peter

A few hints, see below in the code.

> ----------------------------------------------------------------------------------
> structIn=struct("time",[1 2 3],"up",[],"left",[],"speed",[22 33]);

First of all, your "speed" data has only 2 elements, which would cause
a dimension mismatch anyway; I hope it's only a typo.

There may be more suitable ways to organize these data, for example a
non-scalar structure of size [1, 3], enabling you to use struct2cell()
(or maybe even make a cell array from the beginning on) and then
cell2csv(), the latter being able to generate the empty CSV fields if
the cell element is the empty matrix. Or if you want to use matrices,
the structure could contain the field 'headers' = {"time2, "up",
"left", "speed"} and the field 'data', a matrix with the respective

For the case you really want to use matrices, further comments:

> %Get each field name from the structure
> fieldNames = fieldnames(structIn);
> %Create a text string with the field names
> headerOut=sprintf('%s,',fieldNames{:});
> headerOut=headerOut(1:end-1); %Trim off last `,` and `\t`
> %prepare matrix according to size of struct
> matOut=zeros(max(size(structIn.(fieldNames{1}))), length(fieldNames));
> if isempty(structIn.(fieldNames{1}));
>    matOut=[];
> else
>    for i=1:length(fieldNames)

why don't you just test here 'if (! isempty (...))' ?
>   matOut(:,i)=structIn.(fieldNames{i});

>    end
> end
> %Write to csv.
> dlmwrite(‘outfile.csv’, headerOut, '');
> dlmwrite(‘outfile.csv’, matOut, '-append');

If you want to use matrices and 'dlmwrite()' (instead of cell2csv),
you AFAICS can't get empty CSV cells. Maybe you can use the 'NA' ('not
available') value instead, initializing 'matOut' as NA (nrows,
ncolums) ?


public key id EAFE0591, e.g. on x-hkp://

Attachment: signature.asc
Description: Digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]