Combining many variables into one table

I have 100 variables from simulink output all stored in a huge .mat file (after being collated from seperated .mat files). I just want a specific variable from each one in a table. The examples of making a table have a few variables in that you type the names manually to create the table.
T = table (results(1, 1).Collision1.signals.values, results(2, 1).Collision1.signals.values,results(3, 1).Collision1.signals.values, results(4, 1).Collision1.signals.values
I will have 300 so this isnt an option.
How can I get the specific variable out I want :
results(1, 1).Collision1.signals.values
results(2, 1).Collision1.signals.values
results(3, 1).Collision1.signals.values
******
results(99, 1).Collision1.signals.values
results(100, 1).Collision1.signals.values
and plot them all in one table?
(figure 1, variables from .mat resullts workspace)
Many thanks
Erin

回答 (2 件)

Benjamin Thompson
Benjamin Thompson 2022 年 3 月 10 日

0 投票

You can use addvars in a for loop to successively add columns to a table. Note all table columns will need to have the same number of entries. I am guessing at variable names here but if you have further questions post sample data.
T = table();
for i = 1:N
T = addvars(T, results(i, 1).Collision1.signals.values,['results(' num2str(i) ', 1).Collision1.signals.values']);
end

4 件のコメント

erin wootton
erin wootton 2022 年 3 月 11 日
Hello,
Thank you very much for your reply. Unfortunately I get this error.
Error using tabular/addvars (line 79)
Wrong number of arguments.
Error in import_to_excel (line 6)
T = addvars(T, results(i, 1).Collision1.signals.values,['results(' num2str(i) ', 1).Collision1.signals.values']);
but thank you again for your reply.
Kind regards,
Erin
Benjamin Thompson
Benjamin Thompson 2022 年 3 月 11 日
You can always type "help tabular/addvars" or "doc tabular/addvars" for more information. I missed the 'NewVariableNames' argument that was needed, and I imagine you will need to make additional adjustments to suit your needs. If you have further questions please post code and data.
T = addvars(T, results(i, 1).Collision1.signals.values, 'NewVariableNames', ['results(' num2str(i) ', 1).Collision1.signals.values']);
Peter Perkins
Peter Perkins 2022 年 3 月 11 日
This will probably work, but will create some pretty awkward variable names. I suggest something more like ['X' num2str(i)]. Also, addvars is fine, but
T.(['X' num2str(i)]) = results(i, 1).Collision1.signals.values
might be easier to read.
Benjamin Thompson
Benjamin Thompson 2022 年 3 月 11 日
Yes the OP is free to use some alternative variable naming scheme.

サインインしてコメントする。

Peter Perkins
Peter Perkins 2022 年 3 月 11 日

0 投票

In recent MATLAB version, there's Simulink.Simulation.Dataset called extractTimetable. Not sure if that's what you have, but it might help.

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

製品

リリース

R2021b

質問済み:

2022 年 3 月 10 日

コメント済み:

2022 年 3 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by