MATLAB Answers

Concatenate a string array and a table of different dimensions

4 views (last 30 days)
I have a txt which that has two parts (see the txt attached): an non-editable header (in the file attached, everyline that has a # as first charater), followed by a tab-delimited table. To replace some of the values in the table, I have imported the table in matlab by doing the following:
table = readtable('data.txt');
The command above will skip the header section of the file and import the table as expected. I then replaced the values.
Problem: What I need to do now, is to paste the table below the header section of the original file and save it as a txt file. This part is essential for the file to be properly read in the package I'm using to run my analyses. This is where I got stuck and can't seem to find a way out. It seems to me that the header can only be imported in MATLAB as a string array by doing the following:
header = readlines('data.txt');
header = header(1:24, :);
So, I have been trying to turn the table into a string array, so it could be pasted below header. On the one hand, if I try to do that:
tableArray = table2array(table);
The tableArray object has more dimensions (= columns) that the header object, so they can't be concatenated. On the other hand, if I try to reduce the dimensions of tableArray, the columns of the tables are merged together, which will make it impossible to use the file for later analyses.
I am not a Matlab expert and I've run out of options. I would really appreciate some help -- even a suggestion -- with this.
Thank you in advance!

Accepted Answer

Seth Furman
Seth Furman on 9 Mar 2021
writematrix and writetable both take a 'WriteMode' name-value pair that can be set to 'append'. To write a header followed by a table, you can use writematrix to write your file header and then writetable(__,'WriteMode','append') to append the table data to the same file.
For example,
>> t = readtable('patients.xls');
>> t = head(t)
t =
8×10 table
LastName Gender Age Location Height Weight Smoker Systolic Diastolic SelfAssessedHealthStatus
____________ __________ ___ _____________________________ ______ ______ ______ ________ _________ ________________________
{'Smith' } {'Male' } 38 {'County General Hospital' } 71 176 true 124 93 {'Excellent'}
{'Johnson' } {'Male' } 43 {'VA Hospital' } 69 163 false 109 77 {'Fair' }
{'Williams'} {'Female'} 38 {'St. Mary's Medical Center'} 64 131 false 125 83 {'Good' }
{'Jones' } {'Female'} 40 {'VA Hospital' } 67 133 false 117 75 {'Fair' }
{'Brown' } {'Female'} 49 {'County General Hospital' } 64 119 false 122 80 {'Good' }
{'Davis' } {'Female'} 46 {'St. Mary's Medical Center'} 68 142 false 121 70 {'Good' }
{'Miller' } {'Female'} 33 {'VA Hospital' } 64 142 true 130 88 {'Good' }
{'Wilson' } {'Male' } 40 {'VA Hospital' } 68 180 false 115 82 {'Good' }
>> writematrix(["BEGIN HEADER";"Header line 1";"Header line 2";"END HEADER";""],'myfile.txt')
>> type myfile.txt
BEGIN HEADER
Header line 1
Header line 2
END HEADER
>> writetable(t,'myfile.txt','WriteMode','append','WriteVariableNames',true);
>> type myfile.txt
BEGIN HEADER
Header line 1
Header line 2
END HEADER
LastName,Gender,Age,Location,Height,Weight,Smoker,Systolic,Diastolic,SelfAssessedHealthStatus
Smith,Male,38,County General Hospital,71,176,1,124,93,Excellent
Johnson,Male,43,VA Hospital,69,163,0,109,77,Fair
Williams,Female,38,St. Mary's Medical Center,64,131,0,125,83,Good
Jones,Female,40,VA Hospital,67,133,0,117,75,Fair
Brown,Female,49,County General Hospital,64,119,0,122,80,Good
Davis,Female,46,St. Mary's Medical Center,68,142,0,121,70,Good
Miller,Female,33,VA Hospital,64,142,1,130,88,Good
Wilson,Male,40,VA Hospital,68,180,0,115,82,Good
>> readtable('myfile.txt')
ans =
8×10 table
LastName Gender Age Location Height Weight Smoker Systolic Diastolic SelfAssessedHealthStatus
____________ __________ ___ _____________________________ ______ ______ ______ ________ _________ ________________________
{'Smith' } {'Male' } 38 {'County General Hospital' } 71 176 1 124 93 {'Excellent'}
{'Johnson' } {'Male' } 43 {'VA Hospital' } 69 163 0 109 77 {'Fair' }
{'Williams'} {'Female'} 38 {'St. Mary's Medical Center'} 64 131 0 125 83 {'Good' }
{'Jones' } {'Female'} 40 {'VA Hospital' } 67 133 0 117 75 {'Fair' }
{'Brown' } {'Female'} 49 {'County General Hospital' } 64 119 0 122 80 {'Good' }
{'Davis' } {'Female'} 46 {'St. Mary's Medical Center'} 68 142 0 121 70 {'Good' }
{'Miller' } {'Female'} 33 {'VA Hospital' } 64 142 1 130 88 {'Good' }
{'Wilson' } {'Male' } 40 {'VA Hospital' } 68 180 0 115 82 {'Good' }

More Answers (1)

Steven Lord
Steven Lord on 8 Mar 2021
I'd probably store that information in the Description property of the table.
T = array2table(magic(4));
T.Properties.Description = "A table array created from the magic(4) matrix";
This information is not displayed when the table is displayed normally, but it can be seen using the summary function.
disp(T)
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
summary(T)
Description: A table array created from the magic(4) matrix Variables: Var1: 4x1 double Values: Min 4 Median 7 Max 16 Var2: 4x1 double Values: Min 2 Median 9 Max 14 Var3: 4x1 double Values: Min 3 Median 8 Max 15 Var4: 4x1 double Values: Min 1 Median 10 Max 13
s = T.Properties.Description
s = 'A table array created from the magic(4) matrix'
  1 Comment
Roberto Petrosino
Roberto Petrosino on 9 Mar 2021
Hi, thanks for replying!
If I try to do what you suggested, but I don't think it works for two reasons.
First, data.Properties.Description needs the Description property to be a string or a character vector. On the other hand, the header needs to be extracted by line number from the original file data.txt. This means that the data must be imported in matlab as a string array (I used readlines). If I try to do what you suggested, an error pops up:
header = readlines(/Users/user/Desktop/data.txt');
header = header(1:20,:);
data = readtable(/Users/user/Desktop/data.txt');
data.Properties.Description = header;
> The Description property must be a string or a character vector.
Second, even if I manage to store the header in the Description property of the table, how to do I print the whole thing (header + modified table) in a txt file?
Thanks!!

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by