arrangement problem with writing cellArray to txt file.

2 ビュー (過去 30 日間)
sermet
sermet 2014 年 6 月 30 日
編集済み: dpb 2014 年 6 月 30 日
[fileName, filePath] = uiputfile('*.txt', '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];
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);
%I need to write cellArray into txt file as it look below;
a 32.56744567 33.54543333
b1 32.55546543 33.77786567
3 32.66874567 33.44843753
%my codes writes it horizontally.

採用された回答

dpb
dpb 2014 年 6 月 30 日
編集済み: dpb 2014 年 6 月 30 日
>> [nr nc]=size(cellArray);
>> fmt=['%5s' repmat('%8.3f',1,nc) '\n'];
>> for i=1:nr
fprintf(fmt,cellArray{i,1},[cellArray{i,2:end}]),end
a 32.567 33.545
b1 32.555 33.778
3 32.669 33.448
>>

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by