MATLAB fprintf(): Create Table of Height & Distances Earth & Mars

In summary, the conversation and code discuss creating a table with three columns using fprintf() to print a height vector and distance matrix side by side. The code includes a function to calculate the distance to the horizon and a for loop to print the matrix elements with tabs. The homework problem requires the use of fprintf() even though there are easier ways to achieve the desired result.
  • #1
HAL10000
21
0
I'm trying to create a table with three separate columns using fprintf(). The first column consists of a height vector h = 0:500:10000 and the second and third columns are part of the matrix Distance.
I've tried looking online and I can't seem to find a way to make the vector and the matrix to print side by side. There are easier ways to obtain this result but the homework problem is telling me to use fprintf(). Please help.

This is the code for the function:
Code:
%--------------------------------------------------------
function [ Distance ] = dist_func( h , r )
% dist_func This function calculates the distance to the horizon
%from various heights on different planets.
Distance = sqrt((2.*r.*h) + h.^2);
end
%--------------------------------------------------------
... and the program:
Code:
%--------------------------------------------------------
% Homework Problem 1 This program calculates the distance
%to the horizon from different heights on Earth and Mars.
%
% Define the height matrix values in feet.
h = 0:500:10000;
%
% Define the radius values for Earth and Mars in feet.
radius_earth = 3963;
radius_mars = 2108.5;
%
% Define radius matrix in feet
r = [radius_earth;radius_mars];
%
% Create grid containing all height and radius values in feet.
[hg,rg] = meshgrid(r,h);
% Calculate distance for both radii and convert to miles.
Distance = dist_func(hg,rg)*(1/5280);
% Place data into table format
disp('  Height   Distance on Earth    Distance on Mars')
fprintf('%8.2f\n',h); fprintf('%8.2f %8.2f\n',Distance')
The last line is an attempt at making the table.
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Use a for loop to print the matrix elements. For each print statement, separate the matrix entries by tab, \t.
 

FAQ: MATLAB fprintf(): Create Table of Height & Distances Earth & Mars

What is the purpose of the MATLAB fprintf() function?

The fprintf() function in MATLAB is used to format and print data to the screen or a file. It allows for the creation of formatted tables and other types of output.

How do you use the fprintf() function to create a table in MATLAB?

To create a table using the fprintf() function, you must first define the data that you want to include in the table. Then, use the fprintf() function with appropriate formatting options to print the data in a table format.

Can the fprintf() function be used to create a table of height and distances between Earth and Mars?

Yes, the fprintf() function can be used to create a table of any type of data, including the height and distances between Earth and Mars. As long as the data is properly defined and formatted, the fprintf() function can create a table for it.

What is the advantage of using the fprintf() function to create a table in MATLAB?

The fprintf() function allows for more control over the formatting and layout of the table compared to other methods in MATLAB. It also has options for including headers, column titles, and row labels.

Is it possible to save the table created with the fprintf() function as a file?

Yes, the fprintf() function can be used to directly print the table to a file, or the data can be saved as a variable and then exported to a file using other MATLAB functions.

Back
Top