Skip to content

YlmzCmlttn

Cemalettin Yılmaz Blog

Menu
  • Home
  • About Me
  • Projects
    • Iot-AR
    • Magnifi-AR
    • Smarthome-IOS
    • Others
  • Categories
    • Articles
    • Augmented Reality
    • Capture The Flag
      • Google CTF
        • 2018
    • Embedded Systems
    • IoT
    • Logisim
    • My Essays
    • Nvidia Jetson
      • Jetson TX1
    • Operating Systems
      • Kali
      • Raspbian
      • Ubuntu
    • Personal
    • Programming
      • Arduino
      • C
      • C#
      • Css
      • Html
      • Js
      • Matlab
      • Node.js
      • Python
      • Swift
      • VHDL
    • Projects
      • Embedded Systems
      • Electric
      • IoT
      • IoT-AR
      • Logisim
      • Magnifi-AR
      • Pose Estimation
    • Raspberry Pi
    • Xilinx
    • Others
Menu

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

Posted on November 26, 2018February 20, 2019 by Yılmaz Cemalettin

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

MATLAB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
clear;
clc;
load('hw09Stars.mat');
tabular = '============ ============= ============= ============== ========== ==========';
best = 0;
for i=1:length(ValuableStars)
    Stars(i).starName = ValuableStars{i,1};
    Stars(i).distanceToEarth = ValuableStars{i,2};
    for j=1:size((ValuableStars{i,3}),(1))
        Stars(i).planet(j).planetName = ValuableStars{i,3}{j,1};
        Stars(i).planet(j).distanceToStar = ValuableStars{i,3}{j,2};
        Stars(i).planet(j).diameter = ValuableStars{i,3}{j,3};
        Stars(i).planet(j).waterRatio = ValuableStars{i,3}{j,4};
    end
end
a= input('Enter star name (or a part of it): ','s');
foundit= false;
for c=1:length(Stars)
    for k=1:length(Stars(c).starName)-length(a)+1
        if strcmp(a,Stars(c).starName(k:k+length(a)-1))
            if ~foundit
                fprintf('\n%-12s %-13s %-13s %+14s %+10s %+10s\n','Star','Star to Earth','Planet','Star to planet','Diameter','Water %');
                fprintf('%s\n',tabular);
            end
            foundit= true;
            for d=1:length(Stars(c).planet)
                fprintf('%-12s %11gkm %-13s %-12skm %8.fkm %0.7f%% \n',Stars(c).starName,(Stars(c).distanceToEarth)*9.5000e+12,Stars(c).planet(d).planetName,Stars(c).planet(d).distanceToStar, Stars(c).planet(d).diameter, Stars(c).planet(d).waterRatio*100)
            end
        end
    end
    for e=1:length(Stars(c).planet)
        waterratio = Stars(c).planet(e).waterRatio;
        if (best < waterratio) && (~strcmp(Stars(c).planet(e).planetName,'Earth'))
            best = waterratio;
            whichstar=c;
            whichplanet=e;
        else
        end
    end
end
if ~foundit
    fprintf('Program did not found any Star name contains ''%s''.\n',a);
end
fprintf('The planet with maximum water ratio is %s of the star %s. \n',Stars(whichstar).planet(whichplanet).planetName,Stars(whichstar).starName);
fprintf('It has a water ratio of %0.5f%% and a diameter of %gkm. \n',Stars(whichstar).planet(whichplanet).waterRatio*100,Stars(whichstar).planet(whichplanet).diameter);

 

Leave a Reply Cancel reply

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

My Motto

“Learn to share, Share to learn”

LinkedIn Badge

Cemalettin Yılmaz

Ads

Archives

Categories

  • Articles (1)
  • Augmented Reality (3)
  • Capture The Flag (23)
    • Google CTF (22)
      • 2018 (13)
      • 2019 (9)
    • PicoCTF (1)
      • 2019 (1)
  • Embedded Systems (3)
  • IoT (3)
  • Logisim (1)
  • My Essays (3)
  • Nvidia Jetson (5)
    • Xavier (5)
  • Operating Systems (24)
    • Kali (3)
    • Raspbian (2)
    • Ubuntu (21)
  • Others (1)
  • Personal (1)
  • Programming (44)
    • Arduino (4)
    • C (10)
    • C# (4)
    • C++ (5)
    • Css (1)
    • CUDA (6)
    • Html (1)
    • Js (2)
    • Libraries (5)
      • OpenCV (3)
      • OpenGL (2)
    • Matlab (14)
    • Node.js (5)
    • Python (6)
    • Swift (3)
  • Programs (4)
    • Tools (4)
  • Projects (21)
    • Books Solutions (8)
    • Electric (2)
    • Embedded Systems (2)
    • Energy Harvesting (1)
    • IoT (2)
    • IoT-AR (1)
    • Logisim (1)
    • Magnifi-AR (3)
    • Pose Estimation (3)
    • Smarthome-Ios (1)
  • Raspberry Pi (3)
  • Uncategorized (2)
  • YZlib (1)

Recent Posts

  • atof stof stod problems with local floating point separator in C/C++
  • Pico CTF 2019 Answers
  • YZlib: Personal C++ Library
  • Drive to target | Google CTF 2019
  • FriendSpaceBookPlusAllAccessRedPremium | Google CTF 2019

Recent Comments

  • AbaShelha on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Peter on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Yılmaz Cemalettin on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Yılmaz Cemalettin on 16-Bit CPU on Logisim
  • Jenny on 16-Bit CPU on Logisim
  • MOON on 16-Bit CPU on Logisim
  • anti on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • hunkerjr on STOP GAN | Google CTF 2019
  • Shaq on 16-Bit CPU on Logisim
  • NURUL AFIQAH MOHD HASBULLAH on 16-Bit CPU on Logisim

Linkedln

© 2022 YlmzCmlttn | Powered by Superbs Personal Blog theme