Filter löschen
Filter löschen

Add header to extrated data from .mat file

1 Ansicht (letzte 30 Tage)
Sachin Uttamrao
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
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'})

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Sachin Uttamrao
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.
% Create a table with appropriate variable names
testinput.Properties.VariableNames = ["#time(sec)", "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', 'WriteVariableNames',true);
clear testinput
Adding this line in code , gives correct header name in text file : testinput.Properties.VariableNames = ["#time(sec)", "throttle", "steering", "velocity", "brake"]

Weitere Antworten (1)

R
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
testinput = 100x5 table
time throttle steering velocity brake ________ ________ ________ ________ ________ 0.41452 0.62788 0.25442 0.18533 0.87363 0.98121 0.23083 0.2983 0.032938 0.6416 0.67979 0.42414 0.79698 0.69591 0.45451 0.78929 0.90275 0.020621 0.3601 0.39386 0.77236 0.9769 0.9012 0.99747 0.11586 0.66859 0.31125 0.92999 0.90941 0.33043 0.50543 0.074553 0.91383 0.086296 0.50144 0.21312 0.72086 0.66756 0.018717 0.032215 0.75456 0.31086 0.34233 0.6623 0.1144 0.29715 0.58606 0.85516 0.39297 0.21378 0.14169 0.73537 0.51033 0.79215 0.91107 0.021351 0.64464 0.70889 0.7768 0.38662 0.84093 0.94033 0.78733 0.59366 0.60562 0.99164 0.39022 0.078714 0.029404 0.53313 0.42887 0.84706 0.82858 0.020165 0.27638 0.018183 0.28935 0.60206 0.026263 0.75232
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
Sachin Uttamrao
Sachin Uttamrao am 25 Jun. 2024
Thanks ,
After adding this , it gives following error,
Error using Export_Test_to_cvs_asc_files (line 34)
Writing nested tables/timetables is not supported. Use SPLITVARS on the nested table before writing.
R
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Block Libraries finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by