Writing two Matrices in table format using fprintf

3 vues (au cours des 30 derniers jours)
Chris Naron
Chris Naron le 18 Avr 2016
Modifié(e) : Chris Naron le 18 Avr 2016
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);

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 18 Avr 2016
Modifié(e) : Azzi Abdelmalek le 18 Avr 2016
Use [t;h] instead of [t h]'
saveTxt = fprintf(text_table, '%8.2f %8.3f\n', [t;h])
  1 commentaire
Chris Naron
Chris Naron le 18 Avr 2016
Modifié(e) : Chris Naron le 18 Avr 2016
Wow, I missed the most basic thing. I made an array, not a matrix. Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by