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?

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by