writing cellArray to doc file.
1 view (last 30 days)
Show older comments
[fileName, filePath] = uiputfile('*.doc', 'Create a file:')
if ~ischar(fileName)
return;
end
fileID = fopen(fullfile(filePath, fileName), 'w');
coordinates=[32.56744567,33.54543333;32.55546543,33.77786567;32.66874567,33.44843753];
coordinates=num2cell(coordinates);
ids=[{'a'};{'b1'};{'3'}];
cellArray=[ids,coordinates];
%I need to write cellArray into word doc file with the same precision of coordinates.
0 Comments
Accepted Answer
Geoff Hayes
on 27 Jun 2014
If you are trying to write the cell array data to an MS Word file, then you should check out http://www.mathworks.com/matlabcentral/fileexchange/9112-writetowordfrommatlab in the MATLAB File Exchange for details on how to do this.
If you wish to simply write the data to a text file without loss of precision, then try something like
if fileID>0
for k=1:size(cellArray,1)
for m=1:size(cellArray,2)
% get the data type of the element in the cell array
dataType = class(cellArray{k,m});
% element data type determines how we write it to file
if strcmpi(dataType,'char')
fprintf(fileID,'%s\t',cellArray{k,m});
elseif strcmpi(dataType,'double')
fprintf(fileID,'%.10f\t',cellArray{k,m});
% etc. for each data type in the cell array
end
end
fprintf(fileID,'\n');
end
fclose(fileID);
end
By using the %.10f we are requesting that ten digits beyond the decimal place be written to file.
0 Comments
More Answers (0)
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!