How to automate creating separate timetables?
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 5 Mär. 2025
"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?
Antworten (2)
Star Strider
am 4 Mär. 2025
Bearbeitet: Walter Roberson
am 4 Mär. 2025
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 Kommentare
Star Strider
am 4 Mär. 2025
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)
It remains a string array, as in my first example.
.
Peter Perkins
am 7 Mär. 2025
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)
varNames = tt.Properties.VariableNames
for i = 1:length(varNames)
s.(varNames{i}) = tt(:,i);
end
s
0 Kommentare
Siehe auch
Kategorien
Mehr zu Simulink Functions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!