fprintf cutting off string

5 visualizzazioni (ultimi 30 giorni)
Tessa Hennigh
Tessa Hennigh il 2 Lug 2015
Commentato: Star Strider il 2 Lug 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?

Risposta accettata

Star Strider
Star Strider il 2 Lug 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 Commenti
Tessa Hennigh
Tessa Hennigh il 2 Lug 2015
Works! Thank you so much!
Star Strider
Star Strider il 2 Lug 2015
My pleasure!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by