David Kelley
David Kelley

Reputation: 1448

writetable without dimension names

I'm trying to write a CSV from a table using writetable with writetable(..., 'WriteRowNames', true), but when I do so, Matlab defaults to putting Row in the (1,1) cell of the CSV. I know I can change Row to another string by setting myTable.Properties.DimensionNames{1} but I can't set that to be blank and so it seems like I'm forced to have some text in that (1,1) cell.

Is there a way to leave the (1,1) element of my CSV blank and still write the row names?

Upvotes: 2

Views: 2040

Answers (4)

T Yin
T Yin

Reputation: 1

writetable(T,fileFullPath,'WriteVariableNames',false);

When specify 'WriteVariableNames' as false (default one is true), then the variable/dimension names will NOT be written in the output file.

Ref link: https://uk.mathworks.com/help/matlab/ref/writetable.html

Upvotes: -1

sco1
sco1

Reputation: 12214

Another approach could use memmapfile to modify the leading bytes of the file in memory.

For example:

% Set up data
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(Age, Height, Weight, BloodPressure, 'RowNames', LastName);

% Write data to CSV
fname = 'asdf.csv';
writetable(T, fname, 'WriteRowNames', true)

% Overwrite row dimension name in the first row
% Use memmapfile to map only the dimension name to memory
tmp = memmapfile(fname, 'Writable', true, 'Repeat', numel(T.Properties.DimensionNames{1}));
tmp.Data(:) = 32;  % Change to the ASCII code for a space
clear('tmp');  % Clean up

Which brings us from:

Row,Age,Height,Weight,BloodPressure_1,BloodPressure_2
Smith,38,71,176,124,93
Johnson,43,69,163,109,77
Williams,38,64,131,125,83
Jones,40,67,133,117,75
Brown,49,64,119,122,80

To:

   ,Age,Height,Weight,BloodPressure_1,BloodPressure_2
Smith,38,71,176,124,93
Johnson,43,69,163,109,77
Williams,38,64,131,125,83
Jones,40,67,133,117,75
Brown,49,64,119,122,80

Unfortunately not quite deleted, but it's a fun approach.


Alternatively, you can use MATLAB's low level file IO to copy everything after the row dimension name to a new file, then overwrite the original:

fID = fopen(fname, 'r');
fID2 = fopen('tmp.csv', 'w');
fseek(fID, numel(T.Properties.DimensionNames{1}), 'bof');
fwrite(fID2, fread(fID));
fclose(fID);
fclose(fID2);
movefile('tmp.csv', fname);

Which produces:

,Age,Height,Weight,BloodPressure_1,BloodPressure_2
Smith,38,71,176,124,93
Johnson,43,69,163,109,77
Williams,38,64,131,125,83
Jones,40,67,133,117,75
Brown,49,64,119,122,80

Upvotes: 1

gnovice
gnovice

Reputation: 125854

There doesn't appear to be any way to set any of the character arrays in the 'DimensionNames' field to either empty or whitespace. One option is to create your .csv file as you do above, then use xlswrite to clear that first cell:

xlswrite('your_file.csv', {''}, 1, 'A1');

Even though the xlswrite documentation states that the file argument should be a .xls, it still works properly for me.

Upvotes: 3

souty
souty

Reputation: 607

No, that is currently not supported. The only workaround I see is to use a placeholder as dimension name and to programmatically remove it from the file afterwards.

Upvotes: -1

Related Questions