How to automate creating separate timetables?

4 次查看(过去 30 天)
Hello all,
I'm trying to create separate timetables based on extracting data from a larger matrix. The larger matrix is being generated using for loops, but I couldn't figure out how to generate new variables (TestX_YZ) in each loop iteration, and I read it's not recommended anyway.
I have done that manually so far, but if there are any ideas to automate this process (especially with the data set getting larger), I'd highly appreciate it. Here's a sample of the code I've written to do the job manually:
Test1_11=array2timetable(TestData(:,1),'SampleRate',SampleRateHz);
Test1_12=array2timetable(TestData(:,2),'SampleRate',SampleRateHz);
Test1_13=array2timetable(TestData(:,3),'SampleRate',SampleRateHz);
Test1_14=array2timetable(TestData(:,4),'SampleRate',SampleRateHz);
Test1_15=array2timetable(TestData(:,5),'SampleRate',SampleRateHz);
...
  3 个评论
Ahmed
Ahmed 2025-3-4
编辑:Ahmed 2025-3-4
My need for separate timetables is related to loading them into signalEditor as separate signals/scenarios, and using that in Simulink. Unfortunately signalEditor doesn't allow multidimensional timetables.
I completely understand that dynamically creating variables is not recommended and inefficient, but it's going to be more efficient than manually copying and pasting 50 of these commands, and manually adjusting the variable names :D
The question is, how can I do so?
Stephen23
Stephen23 2025-3-5
"How to automate creating separate timetables?"
"I completely understand that dynamically creating variables is not recommended and inefficient, but it's going to be more efficient than manually copying and pasting 50 of these commands, and manually adjusting the variable names... The question is, how can I do so?"
The documentation states that signals can be a "Structure of MATLAB timeseries or timetable objects."
Did you try that?

请先登录,再进行评论。

回答(2 个)

Star Strider
Star Strider 2025-3-4
编辑:Walter Roberson 2025-3-4
Creating separate variables for each one is definitely not recommended.
One option would be to create them as cell arrays, for example:
for k1 = 1:Whatever1
for k2 = 1:Whatever2
Test{k1,k2} = array2timetable(TestData(:,k2),'SampleRate',SampleRateHZ);
end
end
There may be other approaches. This seems to me to be the most obvious, and most easily implemented.
.
  4 个评论
Ahmed
Ahmed 2025-3-4
编辑:Ahmed 2025-3-4
I meant a timetable with multiple variables/columns. Please check the signals that can be used with Signal Editor here: https://www.mathworks.com/help/simulink/slref/signaleditorblock.html
I tried using a timeseries instead (which is also not recommended to be used and a timetable is preferred), but when loading a multi-variable timeseries into signalEditor, all signals are lumped together and the signalEditor output in Simulink doesn't let me select which signal from the different column to simulate.
That's why I ended up creating separate timetables.
Star Strider
Star Strider 2025-3-4
I don’t understand.
The only difference between your code example and my version of it is the indexing of the original timetable arrays as elements of a cell array. The advantage that my code offers is that it is a straighttforward way of indexing your tables without creating individual new variables.
I have Simullink, however I rarely use it currently and have never used the Signal Editor block. From my reading of that documentation, what that block wants is actually a .mat file for each signal. In order to do that, my code changes to:
for k1 = 1:Whatever1
for k2 = 1:Whatever2
tt = array2timetable(TestData(:,k2),'SampleRate',SampleRateHZ);
filename(k1,k2) = ["Test"+k1+"_"+k2+".mat"]
save(filename(k1,k2),'tt')
end
end
The extra line that defines the .mat file names simply gives you a straightforward way of finding them and working with them. You can use that reference to provide the filenames to the block. See the documentation section on File name for those details.
An alternative approach to the filename would be:
filename(k1,k2) = sprintf("Test%02d_%02d",k1,k2)
That would insert leading zeros for single digit values of ‘k1’ and ‘k2’, for example —
k1 = 1;
k2 = 11;
filename(k1,k2) = sprintf("Test%02d_%02d",k1,k2);
filename(k1,k2)
ans = "Test01_11"
It remains a string array, as in my first example.
.

请先登录,再进行评论。


Peter Perkins
Peter Perkins 2025-3-7
As Steve says, "The documentation states that signals can be a "Structure of MATLAB timeseries or timetable objects." Does this help?
X = rand(10,5);
tt = array2timetable(X,SampleRate=1)
tt = 10x5 timetable
Time X1 X2 X3 X4 X5 _____ ________ _________ _______ _______ ________ 0 sec 0.87877 0.21789 0.53691 0.35512 0.16079 1 sec 0.045712 0.80786 0.49763 0.67483 0.80661 2 sec 0.74115 0.16217 0.79375 0.08399 0.010254 3 sec 0.63341 0.68058 0.64789 0.96002 0.21177 4 sec 0.34186 0.40133 0.34071 0.67012 0.40378 5 sec 0.42319 0.35446 0.69305 0.63662 0.99946 6 sec 0.53082 0.79517 0.31434 0.64664 0.62491 7 sec 0.53155 0.43744 0.96477 0.90185 0.59736 8 sec 0.47073 0.67338 0.22334 0.8581 0.39568 9 sec 0.038269 0.0096788 0.91534 0.11409 0.062523
varNames = tt.Properties.VariableNames
varNames = 1x5 cell array
{'X1'} {'X2'} {'X3'} {'X4'} {'X5'}
for i = 1:length(varNames)
s.(varNames{i}) = tt(:,i);
end
s
s = struct with fields:
X1: [10x1 timetable] X2: [10x1 timetable] X3: [10x1 timetable] X4: [10x1 timetable] X5: [10x1 timetable]

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by