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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by