how to read string data from excel and write it to a text file delimited
조회 수: 5 (최근 30일)
이전 댓글 표시
hi all
i have a column of data that i imported using xlsread from excel but i cant write it into a text file because dlmwrite only accepts numerical data and its putting a comma on every character. i also cant use fprintf because it doesnt accept "cell arrays". at the same time i want the data to be delimited by a tab or comma or anything so that i can link the text file to access and make a proper table.
댓글 수: 1
답변 (1개)
Kirby Fears
2015년 12월 3일
So you have a cell array in Matlab that you want to print to csv format...
If your cell array contains only numeric data, dlmwrite() would work fine. Just convert your cell array to a double array with cell2mat() before using dlmwrite().
If your cell array contains numeric data as well as character arrays, first convert all contents to character arrays like so:
c = {'abc','d','efgh'; 12, 34, 56; 78, 9, 10}; % sample data
c = cellfun(@(cElt) num2str(cElt),c,'UniformOutput',false);
Now assuming you have a cell array where each cell contains a character array, you can print them in comma-separated format in a csv file as follows:
fid = fopen('test.csv','w'); % open a new csv file
for row = 1:size(c,1),
if numel(c(row,:))>1,
fprintf(fid,'%s,',c{row,1:end-1});
end
fprintf(fid,'%s\n',c{row,end});
end
fclose(fid);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!