Formatted output & structures: Stars and their planets | Matlab Examples

Matlab Tutorials | Examples

Practice 9:

Formatted output & structures: Stars and their planets

MAT files are used for saving MATLAB variables for later use, or for transferring them between computers. You can save your variables into a MAT file by giving the save command. Example:

>>save(‘myvariables.mat’);

In order to save specific variables (rather than all), you can list the variables to be saved by providing their names in quotations after the file name. Example for saving two variables p and q:

>> save(‘myvariables.mat’,’p’,’q’);
The contents of MAT files are retrieved into MATLAB

workspace by making use of the load command. Example:

>> fileName= ‘stars.mat’;

>> load(fileName);

The MAT file hw09Stars.mat contains a cell array matrix variable named ValuableStars that keeps information about various stars and their planets.

This cell array matrix contains three columns. Every row is allocated to a different star; as more stars can be added to this database in time, assume the number of rows is not fixed. In every row, the first column is the star’s name and the second column is its distance to Earth in light years (assume 1 light year = 9,500,000,000,000 km). The third column contains information about the star’s planets. This information is in the form of an embedded (inner) cell array of four columns.

The embedded cell arrays have the following structure: The first column contains the planet name; the second column is the distance to its star in kilometers; the third column is its diameter in kilometers; the fourth column is its water ratio. Every star

may have a different number of planets; therefore the number of rows of the inner cell array is not fixed.

The below schema shows the structure of a row in the ValuableStars cell array:

Star name

Distance to Earth

Planet name

Distance to its star

Diameter

Water ratio

Planet name

Distance to its star

Diameter

Water ratio

Planet name

Distance to its star

Diameter

Water ratio

Write a MATLAB program that does the following jobs:

  •  Reads the cell array from the hw09Stars.mat file.
  • Copies the cell array’s content into a record structure array variable which has the following fields:
    • starName
    • distanceToEarth
    • planet.planetName
    • planet.distanceToStar
    • planet.diameter
    • planet.waterRatio

Note that as there are possibly many planets of each star, the field planet needs to be an array itself!

  • Gets the name of a star (or a part of it) from the user. Then, finds all the stars whose names contain that string. For example, in the given file, the substring “meda” matches “Andromedae A” and the substring “is” matches “54 Piscium” and “61 Virginis”.
  • Prints the information about the planets of the matching star(s) in a tabular form exactly as in the sample format. Hint: In order to print the ‘%’ character use ‘%%’.
  • Scans all planets of all stars in the record structure array and finds the one with the highest water ratio, excluding Earth. Displays this information exactly as in the sample format.

 

MAT File : https://drive.google.com/open?id=1lee56WbmyHeFgmSt46QbJvskDgINsJZk

Solutions

 

Leave a Reply

Your email address will not be published. Required fields are marked *