Yr Mnth LowTemp(°F) HighTemp(°F) Precip(in) 89 Feb 53.8 71.6 2.67 89 Mar 58.5 76
ID: 3683340 • Letter: Y
Question
Yr Mnth LowTemp(°F) HighTemp(°F) Precip(in)
89 Feb 53.8 71.6 2.67
89 Mar 58.5 76.3 2.84
89 Apr 62.4 80.6 1.80
89 May 68.9 86.3 2.85
Problem 1. Write a program that reads this data and creates and writes to a new ASCII text file
that is equivalent but has the year values converted to be four digit (e.g. 1989). The names of
both files should be provided by the user.
2. Write a program that will read in and plot data from either of the above two types of ASCII
files. Specifically, it should generate a figure with two subplots, one that plots both high and low
temperatures versus time, and one that plots the precipitation versus time. Be sure to label your
figure well and use good practices. (Note that you do not have to convert the year/month to a
meaningful numerical value to do this. For the x-axis plotting values, just create a vector that is
the same size as the y-axis data being plotted, with values from 1 to the number of months.)
Explanation / Answer
Sample code to write a data into textfile
Create a sample table, T.
Name = {'M4';'M5';'M6';'M8';'M10'};
Pitch = [0.7;0.8;1;1.25;1.5];
Shape = {'Pan';'Round';'Button';'Pan';'Round'};
Price = [10.0;13.59;10.50;12.00;16.69];
Stock = [376;502;465;1091;562];
T = table(Pitch,Shape,Price,Stock,'RowNames',Name)
T =
Pitch Shape Price Stock
_____ ________ _____ _____
M4 0.7 'Pan' 10 376
M5 0.8 'Round' 13.59 502
M6 1 'Button' 10.5 465
M8 1.25 'Pan' 12 1091
M10 1.5 'Round' 16.69 562
The table has both column headings and row names.
To Export the table, T, to a text file named tabledata.txt.
writetable(T,'tabledata.txt','Delimiter',' ','WriteRowNames',true)
To View the file.
type tabledata.txt
Sample code to read data from a textfile
If there is a table T.
T =
Pitch Shape Price Stock
_____ ________ _____ _____
M4 0.7 'Pan' 10 376
M5 0.8 'Round' 13.59 502
M6 1 'Button' 10.5 465
M8 1.25 'Pan' 12 1091
M10 1.5 'Round' 16.69 562
T contains one variable for each column in the file and readtable treats the entries in the first line of the file as variable names.
Create a table from the comma-separated text file. Format the first variable as string, second as floating-point numbers, the third variable as a string,the fourth as a floating-point numbers, and the last as a uint32.
T = readtable('tabledata.txt','Format','%s %f %s %f%u')
Sample code to plot the data of a text file
Suppose you had a simple ASCII file named tabledata.txt that contained two columns of numbers.
The following MATLAB statements will load this data into the matrix ``my_matrix'', and then copy it into two vectors, x and y.
>> load my_matrix.dat; % read data into the my_matrix matrix
>> x = my_matrix(:,4); % copy fourth column of my_matrix into x
>> y = my_matrix(:,5); % and fifth column into y
A simple plot of data from a file
>> load tabledata.txt; % read data into tabledata matrix
>> price = tabledata(:,4); % copy fourth column of tabledata into price
>> stock = tabledata(:,5); % and fifth column into tabledata into stock
>> plot(price,stock,'o'); % plot price vs. stock with circles
>> xlabel('price of data'); % add axis labels and plot title
>> ylabel('stock of data');
>> title('sample plot');
note-the above example can help to solve the given question.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.