How to save a text file with headers?

조회 수: 25 (최근 30일)
Dorsa Mir Norouzi
Dorsa Mir Norouzi 2020년 8월 24일
댓글: dpb 2020년 8월 25일
For context, the full code reads data from a text file, calibrates and converts the data to degrees, and then saves the new data in 4 columns with 108766 rows each. This is what I am currently using to save the new text file:
function calibratedData = applyCal(fname,Ey,components,rawData,Calib)
% applies calibration (gain and shift) to raw data; function returns
% calibrated data ONLY for 'components'!
calibratedData = [];
for i = find(components)
calibratedData(:,Ey(i).datacolumn) = ( rawData(:,Ey(i).datacolumn) - Calib(1,Ey(i).datacolumn) ) * Calib(2,Ey(i).datacolumn);
end
%save as textfile:
s = strtok(fname,'.');
SaveName = [s, datestr(now, ' yyyymmddTHHMMSS'),' calibrated.txt'];
save (SaveName, 'calibratedData', '-ASCII');
end
This works - but I need there to be a header at the top of each column. The headers need to be "LE Horizontal" "LE Vertical" "RE Horizontal" and "RE Vertical". Any help would be appreciated! Thank you!
  댓글 수: 3
Dorsa Mir Norouzi
Dorsa Mir Norouzi 2020년 8월 24일
I tried to do this, but I get an error saying that my array "calibratedData" is undefined. But it is defined... so I'm not sure what's going on.
Adam Danz
Adam Danz 2020년 8월 24일
편집: Adam Danz 2020년 8월 24일
That's not a problem with any Matlab functions. That indicates that you're building the table in the wrong place, specifically, before the vcalibratedData variable is defined or, perhaps, in a different workspace.

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2020년 8월 24일
function calibratedData = applyCal(fname,Ey,components,rawData,Calib)
% applies calibration (gain and shift) to raw data; function returns
% calibrated data ONLY for 'components'!
calibratedData = [];
for i = find(components)
calibratedData(:,Ey(i).datacolumn) = ( rawData(:,Ey(i).datacolumn) - Calib(1,Ey(i).datacolumn) ) * Calib(2,Ey(i).datacolumn);
end
%save as textfile:
s = strtok(fname,'.');
SaveName = [s, datestr(now, ' yyyymmddTHHMMSS'),' calibrated.txt'];
names={'"LE Horizontal"','"LE Vertical"','"RE Horizontal"','"RE Vertical"'};
writecell(names,SaveName,'delimiter','space');
save(SaveName, 'calibratedData', '-ASCII' '-append');
end
saves having to create the table just for the purpose of writing a header line. Second time come up in last couple of days...see
  댓글 수: 12
Dorsa Mir Norouzi
Dorsa Mir Norouzi 2020년 8월 25일
Okay, last update. So looks like the solution was much more simple than I thought... Here is the final code that ended up working for me. Thank you again for all your help, it was very beneficial!!
function calibratedData = applyCal(fname,Ey,components,rawData,Calib)
% applies calibration (gain and shift) to raw data; function returns
% calibrated data ONLY for 'components'!
calibratedData = [];
for i = find(components)
calibratedData(:,Ey(i).datacolumn) = ( rawData(:,Ey(i).datacolumn) - Calib(1,Ey(i).datacolumn) ) * Calib(2,Ey(i).datacolumn);
end
s = strtok(fname,'.');
SaveName = [s, datestr(now, ' yyyymmddTHHMMSS'),' calibrated.txt'];
save (SaveName, 'calibratedData', '-ASCII');
fid=fopen(SaveName,'w'); % create the file for write
fprintf(fid,'%15s %15s %15s %15s %15s\n', 'blank', 'LEH', 'LEV', 'REH', 'REV');
fid=fclose(fid); % close the file w/ header
save(SaveName, 'calibratedData', '-ASCII', '-append');
end
dpb
dpb 2020년 8월 25일
names={'Blank','LE Horizontal','LE Vertical','RE Horizontal','RE Vertical'};
fmt1=[repmat('%15s ',1,numel(names)-1) '%s\n'];
fmt2=[repmat('%15e ',1,numel(names)-1) '%f\n'];
The format width parameter is missing on the last element for each -- there are N-1 w/ trailing blank and one w/o. If you use the width parameter, then you can change that...
fmt1=[repmat('%15s ',1,numel(names)) '\n'];
fmt2=[repmat('%15e ',1,numel(names)) '\n'];
In the remainder
...
fprintf(fid,fmt2,calibratedData.'); % note the .' to write in row-major order to file
fid=fclose(fid); % close the file w/ header
save(SaveName, 'calibratedData', '-ASCII', '-append');
you write the data array twice -- once w/ fprintf with the width specified with fmt2 format string and then again you append the data to the file with save with the default format it uses. Use one but only one; I would recommend fprintf over the save .... -append route.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by