increasing precison of num2str for dlmcell function.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
sermet
am 30 Jun. 2014
Kommentiert: Robert Cumming
am 30 Jun. 2014
function dlmcell(file,cell_array,varargin)
%%<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> %%
% <><><><><> dlmcell - Write Cell Array to Text File <><><><><> %
% <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> %
% Version: 01.06.2010 %
% (c) Roland Pfister %
% roland_pfister@t-online.de %
% ...with many thanks to George Papazafeiropoulos %
% for his corrections and improvements. %
% 1. Synopsis %
% %
% A single cell array is written to an output file. Cells may consist of %
% any combination of (a) numbers, (b) letters, or (c) words. The inputs %
% are as follows: %
% %
% - file The output filename (string). %
% - cell_array The cell array to be written. %
% - delimiter Delimiter symbol, e.g. ',' (optional; %
% default: tab ('\t'}). %
% - append '-a' for appending the content to the %
% output file (optional). %
% %
% 2. Example %
% %
% mycell = {'Numbers', 'Letters', 'Words','More Words'; ... %
% 1, 'A', 'Apple', {'Apricot'}; ... %
% 2, 'B', 'Banana', {'Blueberry'}; ... %
% 3, 'C', 'Cherry', {'Cranberry'}; }; %
% dlmcell('mytext.txt',mycell); %
% %
% <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> %
%%Check input arguments
if nargin < 2
disp('Error - Give at least two input arguments!');
return;
elseif nargin > 4
disp('Error - Do not give more than 4 input arguments!');
return;
end
if ~ischar(file)
disp(['Error - File input has to be a string (e.g. ' ...
char(39) 'output.txt' char(39) '!']);
return;
end;
if ~iscell(cell_array)
disp('Error - Input cell_array not of the type "cell"!');
return;
end;
delimiter = '\t';
append = 'w';
if nargin > 2
for i = 1:size(varargin,2)
if strcmp('-a',varargin{1,i}) == 1
append = 'a';
else
delimiter = varargin{1,i};
end;
end;
end
%%Open output file and prepare output array.
output_file = fopen(file,append);
output = cell(size(cell_array,1),size(cell_array,2));
%%Evaluate and write input array.
for i = 1:size(cell_array,1)
for j = 1:size(cell_array,2)
if numel(cell_array{i,j}) == 0
output{i,j} = '';
% Check whether the content of cell i,j is
% numeric and convert numbers to strings.
elseif isnumeric(cell_array{i,j}) || islogical(cell_array{i,j})
output{i,j} = num2str(cell_array{i,j}(1,1));
% Check whether the content of cell i,j is another cell (e.g. a
% string of length > 1 that was stored as cell. If cell sizes
% equal [1,1], convert numbers and char-cells to strings.
%
% Note that any other cells-within-the-cell will produce errors
% or wrong results.
elseif iscell(cell_array{i,j})
if size(cell_array{i,j},1) == 1 && size(cell_array{i,j},1) == 1
if isnumeric(cell_array{i,j}{1,1})
output{i,j} = num2str(cell_array{i,j}{1,1}(1,1));
elseif ischar(cell_array{i,j}{1,1})
output{i,j} = cell_array{i,j}{1,1};
end;
end;
% If the cell already contains a string, nothing has to be done.
elseif ischar(cell_array{i,j})
output{i,j} = cell_array{i,j};
end;
% Cell i,j is written to the output file. A delimiter is appended for
% all but the last element of each row. At the end of a row, a newline
% is written to the output file.
if j < size(cell_array,2)
fprintf(output_file,['%s',delimiter],output{i,j});
else
fprintf(output_file,'%s\r\n',output{i,j});
end
end;
end;
%%Close output file.
fclose(output_file);
end
%when applying num2str function only 4 numbers after the point (floating point) extracting. Is there a way to increase the precision for doubles?
0 Kommentare
Akzeptierte Antwort
Robert Cumming
am 30 Jun. 2014
Bearbeitet: Robert Cumming
am 30 Jun. 2014
edit
So its not a display format issue, looking at the help for str2num you can see that it indicates that you can specify the precision, i.e.
change your line to:
num2str(cell_array{i,j}(1,1),10)
to make the number have extra decimal places in the string conversion
---------------------------------------------------------------------
Are you sure this isn't a display format issue:
str = '1.23456789'
>> str2num ( str )
ans =
1.2346
>> format long
>> str2num ( str )
ans =
1.234567890000000
3 Kommentare
Robert Cumming
am 30 Jun. 2014
it works for me - on R2014a.
Another (better) solution is to write the number to file directly that way you don't need to do the conversion to a str, e.g:
fprintf ( output_file, '%8.6f%s', number, delimiter )
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!