From: Ignazio G on
Hello,

I am trying to save to a text file two vectors,

in order to have the following result:

ART 32
BGH 21
RRT 56
HHH 66

my string vector is
litt=['ART'; 'BGH'; 'RRT'; 'HHH']
and my scalar vector is
nb=[32; 21; 56; 66]

when trying to save the vector to have them as shown above I use:

fid = fopen('textfile','w');
fprintf(fid,'%s %f\n', litt, nb);
fclose(fid)

but I don't get the expected result,
is there something I need to know about saving files with numbers and strings?
thank you
From: Jan Simon on
"Dear Ignazio!

> I am trying to save to a text file two vectors,
>
> in order to have the following result:
>
> ART 32
> BGH 21
> RRT 56
> HHH 66
>
> my string vector is
> litt=['ART'; 'BGH'; 'RRT'; 'HHH']
> and my scalar vector is
> nb=[32; 21; 56; 66]
>
> when trying to save the vector to have them as shown above I use:
>
> fid = fopen('textfile','w');
> fprintf(fid,'%s %f\n', litt, nb);
> fclose(fid)
>
> but I don't get the expected result,

This is a matrix and not a vector:
litt=['ART'; 'BGH'; 'RRT'; 'HHH']

Instead of writing "not the expected result", you could look at the results and describe it exactly. All needed information can be found there!

Please try this:
fid = fopen('textfile','w');
for i = 1:size(nb, 1)
fprintf(fid, '%s %f\n', litt(i, :), nb(i));
fclose(fid);

Look in the help of FPRINTF for the behaviour for array input.

Kind regards, Jan
From: Ignazio G on
Thanks Jan,

I actually found the answer but couldn't find the way to delete the post.
anyway the code didn't run because the fid name was invalid, I did not convert to string some variables I had put in the file name. Once I put num2str(variable) the code returned what I expected.

"Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <hofi8a$g78$1(a)fred.mathworks.com>...
> "Dear Ignazio!
>
>
> This is a matrix and not a vector:
> litt=['ART'; 'BGH'; 'RRT'; 'HHH']
>
> Instead of writing "not the expected result", you could look at the results and describe it exactly. All needed information can be found there!
>
> Please try this:
> fid = fopen('textfile','w');
> for i = 1:size(nb, 1)
> fprintf(fid, '%s %f\n', litt(i, :), nb(i));
> fclose(fid);
>
> Look in the help of FPRINTF for the behaviour for array input.
>
> Kind regards, Jan