Concatenate a string array and a table of different dimensions
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Roberto Petrosino
el 8 de Mzo. de 2021
Comentada: Roberto Petrosino
el 10 de Mzo. de 2021
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!
0 comentarios
Respuesta aceptada
Seth Furman
el 9 de Mzo. de 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' }
Más respuestas (1)
Steven Lord
el 8 de Mzo. de 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)
summary(T)
s = T.Properties.Description
1 comentario
Ver también
Categorías
Más información sobre Tables en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!