Printing out zeros as empty character for a tex file

3 ビュー (過去 30 日間)
yp78
yp78 2021 年 8 月 18 日
コメント済み: yp78 2021 年 8 月 18 日
I have a double variable named `myData' that contains zero entries. I loop through the elements of myData as follows.
How can I print out the zeros as empty character for a tex file that I am producing?
% Current code
myData=([1,2,0;0,5,6;7,0,9]);
nCol = 4;
format = '%.2f';
for i = 1:size(data,1) % 1 to rows
if i==1
fprintf([' \\\\ \\midrule \\multicolumn{' num2str(nCol) '}{l}{\\bfseries m} \\\\ \\midrule \\\\ \n']);
end
fprintf([num2str(i) ' & ' num2str(myData(i,1), format)...
' & ' num2str(myData(i,2), format) ...
' & ' num2str(myData(i,3), format) '\\\\ \n']);
end
\\ \midrule \multicolumn{4}{l}{\bfseries m} \\ \midrule \\
1 & 1.00 & 2.00 & 0.00\\ 2 & 0.00 & 5.00 & 6.00\\ 3 & 7.00 & 0.00 & 9.00\\
I don't want to see zeros in the ouputs above. The desired outputs:
\\ \midrule \multicolumn{4}{l}{\bfseries m} \\ \midrule \\
1 & 1.00 & 2.00 & \\
2 & & 5.00 & 6.00\\
3 & 7.00 & & 9.00\\

採用された回答

David Hill
David Hill 2021 年 8 月 18 日
s=sprintf([num2str(i) ' & ' num2str(myData(i,1), format)...
' & ' num2str(myData(i,2), format) ...
' & ' num2str(myData(i,3), format) '\\\\ \n']);
fprintf(regexprep(s,'0.00',''));
  1 件のコメント
yp78
yp78 2021 年 8 月 18 日
Thanks so much @David Hill !
It saved enormous time to organise the outputs!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by