Combine variables with different number of element in cell

1 次查看(过去 30 天)
I want to make a three-column .tsv file by using listing values of variables, onsets, durations and names in the paired order.
onsets{1}
ans =
84.0190 156.0190 192.0190 266.0190 84.0190 156.0190 192.0190 266.0190
durations{1}
ans =
2.1060 2.4500 2.3990 2.4350 2.1060 2.4500 2.3990 2.4350
names{1}
ans =
'anticipation1'
All three variables are a 1x12 cell array (yet while onsets and durations have 8 elements in each cell, names only has 1 element in each cell).
How can I make the file appear in the following format (e.g., for all three variables k = 1 and names{1} will be assigned to all the 8 elements in onsets{1} and durations{1}):
onsets durations names
84.0190 2.1060 anticipation1
156.0190 2.4500 anticipation1
192.0190 2.3990 anticipation1
266.0190 2.4350 anticipation1
84.0190 2.1060 anticipation1
156.0190 2.4500 anticipation1
192.0190 2.3990 anticipation1
266.0190 2.4350 anticipation1
I tried the following but it does not work; specifically I'm not sure how to assign the "names" group in paired order and repeat that to all 8 elements of other variables.
data = [onsets{1:12}; durations{1:12}; names{1:12}]

采纳的回答

Bhaskar R
Bhaskar R 2019-12-23
编辑:Bhaskar R 2019-12-23
You can write the data using tablewrite,
onsets{1} = [84.0190 156.0190 192.0190 266.0190 84.0190 156.0190 192.0190 266.0190]';
durations{1} = [2.1060 2.4500 2.3990 2.4350 2.1060 2.4500 2.3990 2.4350]';
names{1} = repmat('anticipation1', 8,1);
% create table
t = table(onsets{1},durations{1}, names{1}, 'VariableNames', {'onsets','durations','names'});
% write data to tsv file as
writetable(t, 'output_file.tsv', 'FileType','text','Delimiter','\t' ); % assumed file name is "output_file.tsv"

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by