Place data within a cell in rows

1 Ansicht (letzte 30 Tage)
DavidL88
DavidL88 am 13 Jan. 2022
Bearbeitet: Voss am 14 Jan. 2022
I have a table like below where the results data contained in the cell is a string. Is there a way to stack the data within the results cells in rows? I would like to write this table into Excel as table for publication.
OLD TABLE
Left cuneus Left entorhinal
Experiment 1 t = -3.66 p = 0.001 x, y, z = -26.1, -6.2, 52.2 Nvx = 31 t = -3.38 p = 0.002 x, y, z = -48.6, -43.7, 0.7 Nvx = 20
Experiment 2 t = -4.66 p = 0.002 x, y, z = -46.1, -7.2, 52.2 Nvx = 21 t = -7.38 p = 0.003 x, y, z = -48.6, -63.7, 1.7 Nvx = 22
NEW TABLE
Left cuneus Left entorhinal
Experiment 1 t = -3.66 t = -3.38
p = 0.001 p = 0.002
x, y, z = -26.1, -6.2, 52.2 x, y, z = -48.6, -43.7, 0.7
Nvx = 31 Nvx = 20
Experiment 2 t = -4.66 t = -7.38
p = 0.002 p = 0.003
x, y, z = -46.1, -7.2, 52.2 x, y, z = -48.6, -63.7, 1.7
Nvx = 21 Nvx = 22

Akzeptierte Antwort

Voss
Voss am 13 Jan. 2022
The attached function may be of some use.
t = table(["t = -3.66 p = 0.001 x, y, z = -26.1, -6.2, 52.2 Nvx = 31"; "t = -4.66 p = 0.002 x, y, z = -46.1, -7.2, 52.2 Nvx = 21"],["t = -3.38 p = 0.002 x, y, z = -48.6, -43.7, 0.7 Nvx = 20"; "t = -7.38 p = 0.003 x, y, z = -48.6, -63.7, 1.7 Nvx = 22"],'variablenames',{'Left_cuneus','Left_entorhinal'})
t = 2×2 table
Left_cuneus Left_entorhinal ____________________________________________________________ __________________________________________________________ "t = -3.66 p = 0.001 x, y, z = -26.1, -6.2, 52.2 Nvx = 31" "t = -3.38 p = 0.002 x, y, z = -48.6, -43.7, 0.7 Nvx = 20" "t = -4.66 p = 0.002 x, y, z = -46.1, -7.2, 52.2 Nvx = 21" "t = -7.38 p = 0.003 x, y, z = -48.6, -63.7, 1.7 Nvx = 22"
stack_string(t{1,1})
ans = 4×1 string array
"t = -3.66" "p = 0.001" "x, y, z = -26.1, -6.2, 52.2" "Nvx = 31"
stack_string(t{2,1})
ans = 4×1 string array
"t = -4.66" "p = 0.002" "x, y, z = -46.1, -7.2, 52.2" "Nvx = 21"
stack_string(t{1,2})
ans = 4×1 string array
"t = -3.38" "p = 0.002" "x, y, z = -48.6, -43.7, 0.7" "Nvx = 20"
stack_string(t{2,2})
ans = 4×1 string array
"t = -7.38" "p = 0.003" "x, y, z = -48.6, -63.7, 1.7" "Nvx = 22"
  2 Kommentare
DavidL88
DavidL88 am 14 Jan. 2022
Bearbeitet: DavidL88 am 14 Jan. 2022
Hi Benjamin,
That's great thanks. How do I then place the stacked string back in the original table cell?
I tried t(1,1) = stack_string(t{1,1}) but I get "To assign to or create a variable in a table, the number of rows must match the height of the table." After stacking them I would like to export the table to Excel in this format.
Voss
Voss am 14 Jan. 2022
Bearbeitet: Voss am 14 Jan. 2022
I'm not sure about that; I never use tables. I would use a cell array of character vectors or a 2D string array and write that to file.
variable_names = {'Left cuneus' 'Left entorhinal'};
t = table(["t = -3.66 p = 0.001 x, y, z = -26.1, -6.2, 52.2 Nvx = 31"; "t = -4.66 p = 0.002 x, y, z = -46.1, -7.2, 52.2 Nvx = 21"],["t = -3.38 p = 0.002 x, y, z = -48.6, -43.7, 0.7 Nvx = 20"; "t = -7.38 p = 0.003 x, y, z = -48.6, -63.7, 1.7 Nvx = 22"],'variablenames',strrep(variable_names,' ','_'))
t = 2×2 table
Left_cuneus Left_entorhinal ____________________________________________________________ __________________________________________________________ "t = -3.66 p = 0.001 x, y, z = -26.1, -6.2, 52.2 Nvx = 31" "t = -3.38 p = 0.002 x, y, z = -48.6, -43.7, 0.7 Nvx = 20" "t = -4.66 p = 0.002 x, y, z = -46.1, -7.2, 52.2 Nvx = 21" "t = -7.38 p = 0.003 x, y, z = -48.6, -63.7, 1.7 Nvx = 22"
N_variables = numel(variable_names);
N_experiments = 2;
s_out = repmat("",5*N_experiments,N_variables+1);
s_out(1,2:end) = string(variable_names);
for i = 1:N_experiments
s_out(5*(i-1)+2,1) = "Experiment " + i;
for j = 1:N_variables
s_out(5*(i-1)+2:5*i,j+1) = stack_string(t{i,j});
end
end
s_out
s_out = 10×3 string array
"" "Left cuneus" "Left entorhinal" "Experiment 1" "t = -3.66" "t = -3.38" "" "p = 0.001" "p = 0.002" "" "x, y, z = -26.1, -6.2, 52.2" "x, y, z = -48.6, -43.7, 0.7" "" "Nvx = 31" "Nvx = 20" "" "" "" "Experiment 2" "t = -4.66" "t = -7.38" "" "p = 0.002" "p = 0.003" "" "x, y, z = -46.1, -7.2, 52.2" "x, y, z = -48.6, -63.7, 1.7" "" "Nvx = 21" "Nvx = 22"
writematrix(s_out,'test.xlsx');
readtable('test.xlsx')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
ans = 9×3 table
Var1 LeftCuneus LeftEntorhinal ________________ _______________________________ _______________________________ {'Experiment 1'} {'t = -3.66' } {'t = -3.38' } {0×0 char } {'p = 0.001' } {'p = 0.002' } {0×0 char } {'x, y, z = -26.1, -6.2, 52.2'} {'x, y, z = -48.6, -43.7, 0.7'} {0×0 char } {'Nvx = 31' } {'Nvx = 20' } {0×0 char } {0×0 char } {0×0 char } {'Experiment 2'} {'t = -4.66' } {'t = -7.38' } {0×0 char } {'p = 0.002' } {'p = 0.003' } {0×0 char } {'x, y, z = -46.1, -7.2, 52.2'} {'x, y, z = -48.6, -63.7, 1.7'} {0×0 char } {'Nvx = 21' } {'Nvx = 22' }
readcell('test.xlsx')
ans = 10×3 cell array
{1×1 missing } {'Left cuneus' } {'Left entorhinal' } {'Experiment 1'} {'t = -3.66' } {'t = -3.38' } {1×1 missing } {'p = 0.001' } {'p = 0.002' } {1×1 missing } {'x, y, z = -26.1, -6.2, 52.2'} {'x, y, z = -48.6, -43.7, 0.7'} {1×1 missing } {'Nvx = 31' } {'Nvx = 20' } {1×1 missing } {1×1 missing } {1×1 missing } {'Experiment 2'} {'t = -4.66' } {'t = -7.38' } {1×1 missing } {'p = 0.002' } {'p = 0.003' } {1×1 missing } {'x, y, z = -46.1, -7.2, 52.2'} {'x, y, z = -48.6, -63.7, 1.7'} {1×1 missing } {'Nvx = 21' } {'Nvx = 22' }

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by