Spacing strings using strcat and fprintf

6 ビュー (過去 30 日間)
Seb
Seb 2015 年 11 月 21 日
コメント済み: Seb 2015 年 11 月 21 日
I am attempting to create an output text file that uses a combination of character arrays, standard arrays, and cell arrays. The only suggestion I've been given is to use a combination of strcat to concatenate the string and then use fprintf to put into a text file. I am having trouble formatting the document to my liking and am very new to Matlab. If I did something similar to the example below, how would I use fprintf to format C such that I had desired spacing for all of the elements? :
A=[0.56,0.88];
B='BS';
C=strcat(B,num2str(A(1,1)),num2str(A(1,2)))
  2 件のコメント
Walter Roberson
Walter Roberson 2015 年 11 月 21 日
What spacing do you desire? And how many decimal points do you want output for the numbers? If a value was (for example) 0.0005182 and you wanted 2 decimal places, then would you want 0.00 as the output or would you want 0.00052 as the output or 0.52E-4 ?
Seb
Seb 2015 年 11 月 21 日
In general, I'm just looking for a procedure. My actual data requires scientific notation, and will involve 10 pieces of data to be concatenation.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2015 年 11 月 21 日
You can use techniques similar to what I showed in this post. Construct a cell array, and any column of it that is not going to be pure numeric, convert the numerics to strings, slam everything together with an fprintf() that has %s for any column that is not pure numeric, and appropriate %d or %e or %f or %g format for any column that is pure numeric.
You can use %15s (or as appropriate for the number) to enforce that (at least) 15 characters will be printed for a string -- this allows you to control the width of the column and helps line up decimal places. Numeric formats can take widths as well.
  1 件のコメント
Seb
Seb 2015 年 11 月 21 日
Thank you very much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by