Appyling format to fprintf function via an array lookup

9 visualizaciones (últimos 30 días)
I have a data file where I would like to automatically edit certain lines based on a user selection. Each line of the file I am editing has a different format. (ex. line 1 may be %4.1f whereas line 2 may be %6.2f)
Since the code is automated, and I don't know which lines the user would like to edit beforehand, I was planning on putting these formats into a string array. However, when I try to call from the array in the fprtinf function I get the following warning: "The function FPRINTF is apparently called with a cell array (argument 2)."
My code looks something like this (dumbed down version):
num_format = {'%4.1f' '%6.2f' '%5.3f'}; fprintf(fid,num_format(i),variable); (where i is [1,3])
Since MATLAB doesn't like my call to my lookup array in the fprintf function, is there another way to do this or correct my code?

Respuesta aceptada

Jan
Jan el 9 de Sept. de 2011
Use curly braces to access cell elements:
num_format = {'%4.1f' '%6.2f' '%5.3f'};
fprintf(fid, num_format{i}, variable);

Más respuestas (0)

Categorías

Más información sobre Data Types en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by