fprintf cutting off string

7 vues (au cours des 30 derniers jours)
Tessa Hennigh
Tessa Hennigh le 2 Juil 2015
Commenté : Star Strider le 2 Juil 2015
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?

Réponse acceptée

Star Strider
Star Strider le 2 Juil 2015
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 commentaires
Tessa Hennigh
Tessa Hennigh le 2 Juil 2015
Works! Thank you so much!
Star Strider
Star Strider le 2 Juil 2015
My pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by