MATLAB Answers

huda nawaf

problem in saving array in txt file

Asked by huda nawaf
on 15 Apr 2012

hi, I want to save this array in file but I did not get what I want


x={'RCIN'    'AFGN'    'RGGA';'RCIN'    'RQDM'    'RGGA'};

in fact my data is more larger, but are of this type.

when run this code:

for i=1:2
  for j=1:3
    fprintf(outfid, '%c ',x{i,j} );
  fprintf(outfid,'\n' );

I get web.txt as

R C I N A F G N R G G A 
R C I N R Q D M R G G A 

But I need it as in array: 'RCIN' 'AFGN'.......

please, who can help me , I suffered from this problem since long time

thanks in advance, huda




No products are associated with this question.

3 Answers

Answer by per isakson
on 15 Apr 2012
 Accepted answer

'%c' is single chracter. Try '%s'.


what i I saved in file, then I want read it . why it read character y character.
I want raed it as:
RXIN AFGN........

what u suggested is saving as i need but when read it , it read character by character while i need each 4 characters as one character


Answer by Image Analyst
on 15 Apr 2012

Looks right to me. You got the first row of the 2 by 3 cell array, which is 12 characters, on the first line, and the second row (second 12 characters) on the second line. That's an array of 2 rows and 12 columns. What's the issue? You also need to add an fclose(outfid);

If, by chance, you don't want the space between the characters, then just don't add one:

fprintf(outfid, '%c', x{i,j});

  1 Comment

but i need it array with two rows and three columns as in array above

Answer by Jan Simon
on 15 Apr 2012

x = {'RCIN'    'AFGN'    'RGGA';'RCIN'    'RQDM'    'RGGA'};
outfid = fopen('web.txt','wt');
if outfid == -1, error('Cannot open file'); end  % Always check!
xt = transpose(x);
fprintf(outfid, '%s %s %s\n', xt{:});

  1 Comment

thanks jan
but this is just example 2*3
what if have large dataset?

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!