Add header to extrated data from .mat file
3 views (last 30 days)
Show older comments
Sachin Uttamrao
on 25 Jun 2024
Answered: Sachin Uttamrao
on 26 Jun 2024
%-----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 Comment
Mathieu NOE
on 25 Jun 2024
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'})
Accepted Answer
More Answers (1)
R
on 25 Jun 2024
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 Comments
R
on 25 Jun 2024
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
See Also
Categories
Find more on Electrophysiology in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!