fprintf cutting off string
7 ビュー (過去 30 日間)
古いコメントを表示
I am trying to use fprintf to make a text file of the variable a which is an 853x3 cell. a{:,1} is filled with strings containing two letters followed by three numbers followed by a letter. a{:,2} and a{:,3} are filled with numbers.
I want my text file to look like this:
aa111a 1.1111e+1 1.1111e+1
bb222b 2.2222e+2 2.2222e+2
...
Using:
removedIso.fileID = fopen('removedIso.txt','w');
fprintf(removedIso.fileID,'%s %1.4e %1.4e \n',a{:,:});
I get a text file where the fist two letters of each string have been removed and there are 2 numbers following the remaining part of the string neither of which are a{:,2} or a{:,3}, and a{:,1} and a{:,3} are crammed at the bottom of the file:
11a 7.6000e+01 1.0500e+02
22b 6.6000e+01 1.0100e+02
...
Anyone have any idea why Matlab is cutting off the letters at the beginning of the string and where these random numbers are coming from?
0 件のコメント
採用された回答
Star Strider
2015 年 7 月 2 日
You have to print mixed-class (such a string and numeric) variables with a loop:
a = {'aa111a' 1.1111e+1 1.1111e+1
'bb222b' 2.2222e+2 2.2222e+2};
for k1 = 1:size(a,1)
fprintf(removedIso.fileID, '%s %10.4e %10.4e\n', a{k1,:})
end
aa111a 1.1111e+01 1.1111e+01
bb222b 2.2222e+02 2.2222e+02
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!