Creating ASCII file with 2 columns
조회 수: 34 (최근 30일)
이전 댓글 표시
Hello, I am trying to create an ASCII file containing 2 columns: Date an Density. I managed to get the code to work well if I create 2 separate m files to each of them, but when I merge them, the filename.txt doesn't retrieve the data correctly. It only shows dates and in the wrong displayNot sure what am I doing wrong ! any help would be highly appreciated ! I have attached the data file and the txt file I am getting, and following is my code:
AirDensity = importfile4('Air_Density.csv', 2, 32);
%Date Data
DateStrings = {'01-07';'02-07';'03-07';'04-07';'05-07';'06-07';'07-07';
'08-07';'09-07';'10-07';'11-07';'12-07';'13-07';'14-07';'15-07';'16-07';
'17-07';'18-07';'19-07';'20-07';'21-07';'22-07';'23-07';'24-07';'25-07';
'26-07';'27-07';'28-07';'29-07';'30-07';'31-07'};
DATE_Q14=datetime(DateStrings,'InputFormat','dd-MM');
%converting the datestring 'DATE_Q14' to cell array of strings
DATE_Q14c = cellstr(DATE_Q14);
%Air Density Data
Density=AirDensity(1:end,1);
FID=fopen('filename.txt','w');
fprintf(FID,'%6s \n',['DATE','DENSITY']);
fprintf(FID,'%6s \n', [DATE_Q14c{:},Density{:}]);
fclose(FID);
댓글 수: 0
채택된 답변
Stephen23
2018년 9월 2일
편집: Stephen23
2018년 9월 2일
Your fprintf calls do not make much sense. In particular, you define each fprintf with one %s format, even though you want to print two columns. But lets look at the char concatenation first. Remember in MATLAB [] is a concatenation operator, so your code
['DATE','DENSITY']
is simply equivalent to this:
'DATEDENSITY'
which is unlikely to be what you intended.
Nowhere in your question can I find any hint of the delimiter that you want to use (e.g. comma, tab, etc.), so I am going to guess that you want a comma, in which case you can fix the first fprintf like this:
fprintf(FID,'%s,%s\n','DATE','DENSITY');
Now to the second fprintf. The main mistake is that you forgot the MATLAB is row-major, so when you concatenate those two vectors together it will run down the first column first, and then down the second column. Simply transpose to get the data in the order that you require. Also when you join them you will need to put them into a temporary variable, otherwise there is no way to get the required comma-separated list in the required order. Also , do not extract the contents as you were doing with {}, but actually concatenate the cell arrays themselves using (), like this:
tmp = [DATE_Q14c(:),Density(:)].';
^^^ ^^^ ^^ you need to transpose a cell array!
Then there is the question of the format string: if you want two columns, why do you only have one format defined? You will need two, one for each column, and the delimiter in between, something like this:
fprintf(FID,'%s,%s\n',tmp{:});
^^^^^ two columns with comma delimiter!
Note that I have assumed that Density is a cell array that contains char/string data. If this is not the case then you will need to pick a suitable fprintf format for that data.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!