Add header to extrated data from .mat file
3 次查看(过去 30 天)
显示 更早的评论
%-----create input files for carmaker maneuvers
for i = 1:length(testProcRes.ManoeuvreResults) % Checking the number of data files inside the main mat file.
testinput (:,1)= testProcRes.ManoeuvreResults(1,i).ProcessedRun.Timeseries (:,91); % physical test time in sec
testinput (:,2) = testProcRes.ManoeuvreResults(1,i).ProcessedRun.Timeseries(:,4); % throttle inputs in percentage
testinput (:,3) = testProcRes.ManoeuvreResults(1,i).ProcessedRun.Timeseries (:,18); % test steering wheel angle in rad
testinput (:,4) = testProcRes.ManoeuvreResults(1,i).ProcessedRun.Timeseries (:,19); % Velocity in m/sec
testinput (:,5) = testProcRes.ManoeuvreResults(1,i).ProcessedRun.Timeseries (:,24); % Brake inputs.
txtname = append(testProcRes.ManoeuvreResults(1,i).ManoeuvreName,'_',string(i),'.asc'); % Storing the file name
writetable(testinput,txtname,'Delimiter',' ','QuoteStrings',true, 'Filetype', 'text') % Export data into text file.
In generated txt file i would like to add header for each column eg testinput(:,1) should have time name , so on refer screenshot for more detail
1 个评论
Mathieu NOE
2024-6-25
you need to convert your array testinput to a table and give it the variable names you want
see the doc for table
or use this approach :
%Convert the array, A, to a table and include variable names.
T = array2table(A,'VariableNames',{'Feet','Inches','Centimeters'})
采纳的回答
更多回答(1 个)
R
2024-6-25
To add headers to each column in the generated text file, you can modify your script to create a table with appropriate variable names before writing it to a file. Here’s how you can do it:
% Sample data structure similar to testProcRes for demonstration
testProcRes.ManoeuvreResults(1).ProcessedRun.Timeseries = rand(100, 100); % Random data for demonstration
testProcRes.ManoeuvreResults(1).ManoeuvreName = 'TestManoeuvre';
%-----create input files for carmaker maneuvers
for i = 1:length(testProcRes.ManoeuvreResults) % Checking the number of data files inside the main mat file.
time = testProcRes.ManoeuvreResults(1,i).ProcessedRun.Timeseries(:,91); % physical test time in sec
throttle = testProcRes.ManoeuvreResults(1,i).ProcessedRun.Timeseries(:,4); % throttle inputs in percentage
steering = testProcRes.ManoeuvreResults(1,i).ProcessedRun.Timeseries(:,18); % test steering wheel angle in rad
velocity = testProcRes.ManoeuvreResults(1,i).ProcessedRun.Timeseries(:,19); % Velocity in m/sec
brake = testProcRes.ManoeuvreResults(1,i).ProcessedRun.Timeseries(:,24); % Brake inputs.
% Create a table with appropriate variable names
testinput = table(time, throttle, steering, velocity, brake);
% Create the filename
txtname = append(testProcRes.ManoeuvreResults(1,i).ManoeuvreName, '_', string(i), '.asc'); % Storing the file name
% Write the table to a text file with headers
writetable(testinput, txtname, 'Delimiter', ' ', 'QuoteStrings', true, 'FileType', 'text');
end
testinput
This will create a table with the variable names time, throttle, steering, velocity, and brake, and then write this table to a text file with these headers included.
Make sure to replace the placeholder variable names with the actual names you want.
2 个评论
R
2024-6-25
You can add the following script before creating the filename to check if any variables in testinput are tables/timetable and then use splitvars to flatten them if necessary:
% Check for nested tables/timetables and flatten if necessary
if any(varfun(@istimetable, testinput, 'OutputFormat', 'uniform'))
testinput = splitvars(testinput);
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Electrophysiology 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!