fprintf cutting off string

7 ビュー (過去 30 日間)
Tessa Hennigh
Tessa Hennigh 2015 年 7 月 2 日
コメント済み: Star Strider 2015 年 7 月 2 日
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?

採用された回答

Star Strider
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
  2 件のコメント
Tessa Hennigh
Tessa Hennigh 2015 年 7 月 2 日
Works! Thank you so much!
Star Strider
Star Strider 2015 年 7 月 2 日
My pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by