Add header to extrated data from .mat file
    조회 수: 10 (최근 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에서 Database Toolbox에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


