Appyling format to fprintf function via an array lookup

8 vues (au cours des 30 derniers jours)
Tristan Wolfe
Tristan Wolfe le 9 Sep 2011
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?

Réponse acceptée

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

Plus de réponses (0)

Catégories

En savoir plus sur Data Types dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by