Appyling format to fprintf function via an array lookup

9 ビュー (過去 30 日間)
Tristan Wolfe
Tristan Wolfe 2011 年 9 月 9 日
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?

採用された回答

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by