how to obtain comma delimited output

조회 수: 1 (최근 30일)
Muhsin
Muhsin 2017년 12월 14일
댓글: Walter Roberson 2017년 12월 14일
Hello; I am inexperienced in coding. can anyone help me please about my attached file. I would like to transpose some some data with a specific format in txt. The data may vary but i have made an example for you to understand it with an example output. The data in sheets and number of sheets in csv may change based on data i have. In attached csv file there is only one sheet for first set, however there are some other sheet in same format, but i could not add to csv file as multiple sheets. Any kind of help is appreciated. Thank you
  댓글 수: 1
Image Analyst
Image Analyst 2017년 12월 14일
csv files can't have "sheets" since they're just flat text files. Only .xlsx files can have sheets.

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

채택된 답변

Walter Roberson
Walter Roberson 2017년 12월 14일
S = fileread('input.csv');
newS = regexprep(S, {'^\*PART[^\n]*\n', '^(\d)'}, {'', '*PART\r\n$1'}, 'lineanchors');
fid = fopen('sample output.txt', 'w');
fwrite(fid, newS);
fclose(fid)
  댓글 수: 6
Muhsin
Muhsin 2017년 12월 14일
편집: Muhsin 2017년 12월 14일
it is not important to have the information of sheets in the data. I just want it have all the data in one txt file. Thank you
Walter Roberson
Walter Roberson 2017년 12월 14일
in_filename = 'input.xlsx';
out_filename = 'output.txt';
[filepath, basename, ext] = fileparts(in_filename);
[status, sheets] = xlsfinfo(in_filename);
if isempty(status)
error('file "%s" is not a readable excel sheet', in_filename);
end
allnum = [];
for idx = 1 : length(sheets)
thissheet = sheets{idx};
num = xlsread(in_filename, thissheet);
allnum = [allnum; num];
end
numcols = size(allnum, 2);
fmt = ['*PART\n', repmat('%f,', 1, numcols-1), '%f\n'];
[fid, msg] = fopen(out_filename, 'wt');
if fid < 0
error('Failed to open output file "%s" because "%s"', outfile, msg);
end
fprintf(fid, fmt, allnum.' ); %transpose is important
fclose(fid);

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by