Hi I am trying to print a table (by just using fprintf or disp); please comment below if you know how to make each line align with its corresponding header as possible. If you know how to move the number in first start at the second line that could be better. Thank you in advance =)
here is my code

 採用された回答

Les Beckham
Les Beckham 2020 年 4 月 16 日

0 投票

This should be close to what you want. However, I have used the logical not, and, and or functions which appear to be diffferent that your definitions of functionNot, functionAnd, and functionOr. You can replace these with calls to your functions.
%start with a clear workspace and command window
clearvars; clc;
%Create two array (x,y)
x=[1 1 1 0 0 0 2 2 2];
y=[1 0 2 1 0 2 1 0 2];
%print the header
fprintf(' x y tNot(x) tAnd(x,y) tOr(x,y)\n');
% print the results
for r=1:9
fprintf('%5d%5d%10d%10d%10d\n', x(r), y(r), not(x(r)), and(x(r), y(r)), or(x(r), y(r)));
end
Result:
x y tNot(x) tAnd(x,y) tOr(x,y)
1 1 0 1 1
1 0 0 0 1
1 2 0 1 1
0 1 1 0 1
0 0 1 0 0
0 2 1 0 1
2 1 0 1 1
2 0 0 0 1
2 2 0 1 1

4 件のコメント

Shiyun Li
Shiyun Li 2020 年 4 月 16 日
Thank you so much! May I ask what is '%5d%5d%10d%10d%10d\n' stands for ? =)
Les Beckham
Les Beckham 2020 年 4 月 17 日
Sure, %5d means format a decimal value with a minimum field width of 5 characters (padded on the left with spaces), similarly, %10d means format a decimal value with a minimum width of 10 characters. That is how you can get a predictable width for the columns. Also, the \n means 'start a new line of output here'.
If this solves your problem, please don't forget to accept the answer. If not, add another comment explaining what isn't working like you need it to.
Shiyun Li
Shiyun Li 2020 年 4 月 17 日
Great!Thank you !!
Les Beckham
Les Beckham 2020 年 4 月 17 日
You are welcome. Glad I could help.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

タグ

質問済み:

2020 年 4 月 16 日

編集済み:

2020 年 4 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by