adding headers when using fprintf to write to text

Hi,
I am writing a matrix to text using the following codes:
fid = fopen('Datav1Call_1993.txt','w+');
for idx = 1:size(DataPaper,1)
line = DataPaper(idx,~isnan(DataPaper(idx,:))); % creates the line of data without NaNs
fprintf(fid,[repmat('%d ',1,length(line)),'\n'],line);
end
fclose(fid);
how can i add headers to the columns?

댓글 수: 1

dpb
dpb 2016년 12월 30일
Write 'em before the loop after you open the file...result will likely be more satisfactory for looking at if you use widths on the field specifications to line things up...

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

 채택된 답변

Image Analyst
Image Analyst 2016년 12월 30일
For example, after fopen() and before the loop:
if fid == -1
errorMessage = sprintf('Error opening Datav1Call_1993.txt')
uiwait(errordlg(errorMessage));
return; % Bail out
end
% Now write header string.
fprintf(fid, 'header 1 header 2 header 3 whatever \n');

댓글 수: 6

when i put
fprintf(fid,'dt ID Year Month Day Obs ROA SigmaROA EqCgta AvgEqCgta ROE0 ...
ROEM1Q ROEM2Q ROEM3Q ROEM4Q ...
ROEM1Yr ROEM2Yr ROEM3Yr ROEComp0 ROECompM1Q ROECompM2Q ROECompM3Q ...
ROECompM4Q ROECompM1Yr ROECompM2Yr ROECompM3Yr TLoans AvgTLoans TA ...
RELoans CILoans DepLoans ConsLoans AgrLoans FGvLoans LLP ...
NPL RGL URGL ALLP GTA LCA LCL LC LCnonFAT ...
LCOff Derivatives ILLoans SLLoans ZScore RWA TedSpread Merger ...
CreditDev TradingDev size \n');
I receive an error "Character vector is not terminated properly".
You cannot continue a string constant with ...
Use
['first',
'second',
'last']
Because you can't continue character constant across line continuation markers...
hdr=['dt ID Year Month Day Obs ROA SigmaROA EqCgta AvgEqCgta ROE0 ' ...
'ROEM1Q ROEM2Q ROEM3Q ROEM4Q ' ...
.
.
.
'CreditDev TradingDev size '];
fprintf(fid,'%s\n',hdr);
NB: Must include the blank between the last substring on the continued line before the first of the following or they'll all run together.
cellstr could be your friend here to make it simpler to code...
thanks that was very helpful
>> ['first',
'second',
'last']
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
>>
Yes, the triple dots are needed Danielle. With them it's like ['first', 'second', 'last'] which equals 'firstsecondlast', a single 1-d character array. Without them it tries to make a 2D array and for that to succeed, all strings need to be the same length because you can't have a 2-D array with a ragged right edge. However this demo works:
s1 = ['first', ...
'second', ...
'last']
s2 = ['first-',
'second',
'last--']
You get
s1 =
firstsecondlast
s2 =
first-
second
last--
Note you get a 1-D character array for the first and a 2-D array for the second. I padded the strings so that all strings had 6 characters so now it can make a 3 by 6 character array.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

질문:

2016년 12월 30일

댓글:

2016년 12월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by