Add header to extrated data from .mat file
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Sachin Uttamrao
am 25 Jun. 2024
Beantwortet: Sachin Uttamrao
am 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 Kommentar
Mathieu NOE
am 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'})
Akzeptierte Antwort
Weitere Antworten (1)
R
am 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 Kommentare
R
am 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
Siehe auch
Kategorien
Mehr zu Block Libraries 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!