Append to exisintg file column-wise, different number of rows, don't add NaNs
11 views (last 30 days)
Show older comments
I'm trying to append a table column-wise (with specific column names, so not a matrix) to an existing TXT. The existing file has fewer rows than the appended dataset. I don't want a bunch of NaNs to be added to square-out the dimensions. (The number of rows and columns is much bigger than in this example, so the NaNs actually add a bunch of space and load time.) My code attemp is below, annotated with where I am stuck. Thanks for any help
data = rand(100,2);
data = array2table(data);
% I have a pre-determined list of column names for this table; code skipped here for simplicity
writetable(data, 'myExistingTable.txt');
data2append = rand(250,6);
data2append = array2table(data2append);
% I have a pre-determined list of column names for this table; code skipped here for simplicity
fid001 = fopen('myExistingTable.txt','a');
% below isn't right
fprintf(fid001,'%f %f %f %f %f %f',data2append);
fclose all;
0 Comments
Answers (1)
dpb
on 18 Apr 2025 at 14:15
While old, ran across seeing if anybody had a solution for the 'Append' option to append to right of used area instead only builtin to the bottom...
"... I don't want a bunch of NaNs to be added to square-out the dimensions. "
The problem is that text files are sequentially written; there is no actual shape of the file on disk, only a series of bytes.
You would have to rewrite the whole file with the two arrays appened while both are available, and then you could append the other data left over as a single column...but, you would also have to save the shape information in order to then reconstruct the data on reading it -- and, unless you store it as 1x2 cell array of vectors of different lengths(*), there will be no way to not have the missing data in a 2D array or a table; MATLAB simply doesn't support that.
(*) You could have a table with the two separately-sized arrays as cell arrays in the table, but that would also be awkward to address.
If a text format is mandatory instead of a native .mat file or a binary file, then probably the most efficient storage would be to use a .csv file with the missing data columns being the string of required delimiters...but doing what you're asking for, specifically, with a sequential text file is just not possible.
0 Comments
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!