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

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

outputting a vector to a text file

Asked by William on 25 Jan 2013

I am creating a sort of "Report Generator" and I have several 512 horizontal vectors of type double numbers I need to display in a chart. I am trying to use a for loop to make a 1 to 512 numbered header and underneath it is the numbers in the 512 vectors I am displaying with a tab inbetween. below is my latest attempt and I am getting several syntax errors with the nested fprintf command. Is there a better way to do this?

here is an example:

Vect type 1 2 3 4 6 6 7 8

--------------------------------

vect one 23 32 34 32 31 43 31 32

and so on....

here is the code:

filename = strcat(Chicklet_serial,'.txt');
fid = fopen(filename, 'w');
fprintf(fid,'Test Name');
for ii = 1:512
    fprintf(fid,fprintf('%d\t',ii));
end

0 Comments

William

Tags

Products

No products are associated with this question.

1 Answer

Answer by Image Analyst on 25 Jan 2013
Accepted answer

Don't put an fprintf inside an fprintf like this:

fprintf(fid,fprintf('%d\t',ii));  % Bad!

Do it like this instead

fprintf(fid,'%d\t',ii);

0 Comments

Image Analyst

Contact us