Appyling format to fprintf function via an array lookup

10 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

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

Più risposte (0)

Categorie

Scopri di più su Data Types in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by