Writing two Matrices in table format using fprintf

2 ビュー (過去 30 日間)
Chris Naron
Chris Naron 2016 年 4 月 18 日
編集済み: Chris Naron 2016 年 4 月 18 日
Hi, I've been racking my head on this and have looked up all the relevant solutions I could find (yes, including the docs).
Edit: Made an array instead of a Matrix. that solved it!
Basically I'm looking to gather two sources of data (time and height) and format them to look like a table in fprintf and write them to a txt file. My issue arises when I attempt to put both matrices in a vertical position; it comes out looking like this.
*note where h is the value of height
1 2
3 4
5 6
h h
h h
h h
Instead of the desired:
1 h
2 h
3 h
4 h
5 h
My code is as follows:
% Time t = 1
t = 1;
% init array h for storing height
h = [];
% first value in h cannot be set to 0 and calculated due to 1-based
% based indexing in matlab. So is assigned the values of t = 0 at h(1)
h(1) = ((9.8/2).*(t.^0)) + ((125 .* 0) + 500);
% begin at t(2) and continue to t(31) such that 30 values of h are
% calculated
for t = 2:31
h(t) = ((9.8/2)*(t^2)) + ((125 * t) + 500);
end
% reassign t from 0:30 such that all values are appropriately displayed
t = [0:30];
% open/create a file named fmt_tbl.txt with read and write permissions.
[text_table, errmsg] = fopen('fmt_tbl.txt', 'wt');
%save fmt_table.txt with fprintf in table format with time v height
saveTxt = fprintf(text_table, '%8.2f %8.3f\n', [t,h]');
% close the file
fclose(text_table);

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 18 日
編集済み: Azzi Abdelmalek 2016 年 4 月 18 日
Use [t;h] instead of [t h]'
saveTxt = fprintf(text_table, '%8.2f %8.3f\n', [t;h])
  1 件のコメント
Chris Naron
Chris Naron 2016 年 4 月 18 日
編集済み: Chris Naron 2016 年 4 月 18 日
Wow, I missed the most basic thing. I made an array, not a matrix. Thanks!

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by