Write a Matlab script to assist in plot reading from a plot
Write a Matlab script to assist in plot reading from a plot that is available as an image file. This shall include Reading the plot from the image file located in the same folder as the main script and then plotting it to a Matlab figure. You will find this easy to do using Matlab\'s i ms how function. An example image file Samnlelmaae. ina is available. Collect calibration data for two points along the x-axis. Prompt the user to click on a point along the x-axis of the plot. Record the location of the clicked point. This will be straightforward using Matlab\'s ginput function. After the click, plot a + sign on the plot at the point that was clicked. Store the handles of any + signs, as you will remove them in a minute. Repeat that for a second point along the x-axis Prompt the user to enter the x values he just clicked, according to the values it is on the graph. Store the value. Repeat the preceding two steps to obtain calibration data for points along the y axis. Remove the plotted + signs. Prompt the user to click on points on the graph to see their values and to right click to quit. For each click, check if it was a left or a right click. If it was a right click, then quit. Otherwise continue below. For each left click, Remove any previously plotted + signs, Plot a + sign at the point the user clicked. Print on a single line on the console the graph values of the clicked-on point. You will have to do some straightforward algebra (fit straight lines to the x and y calibration data you collected) to convert the values returned by ginput to the graph values. The above should work for any graphed image file that Matlab can read. You can search online for example plots for testing. You can even use Matlab to create your own plots and save them as images to use in your testing. Expect that other graph images will be used to test your script. You may assume: The axes in the graph are linear. (Though extending the same idea to logarithmic axes is not that difficult, it is not required for this project.) The x and y axes in the graph are aligned horizontally and vertically. There is no need to adjust for scanned graphs that are tilted.
Solution
function [labels,x,y] = readColData(fname,ncols,nhead,nlrows) % readColData reads data from a file containing data in columns % that have text titles, and possibly other header text % % Synopsis: % [labels,x,y] = readColData(fname) % [labels,x,y] = readColData(fname,ncols) % [labels,x,y] = readColData(fname,ncols,nhead) % [labels,x,y] = readColData(fname,ncols,nhead,nlrows) % % Input: % fname = name of the file containing the data (required) % ncols = number of columns in the data file. Default = 2. A value % of ncols is required only if nlrows is also specified. % nhead = number of lines of header information at the very top of % the file. Header text is read and discarded. Default = 0. % A value of nhead is required only if nlrows is also specified. % nlrows = number of rows of labels. Default = 1 % % Output: % labels = matrix of labels. Each row of lables is a different % label from the columns of data. The number of columns % in the labels matrix equals the length of the longest % column heading in the data file. More than one row of % labels is allowed. In this case the second row of column % headings begins in row ncol+1 of labels. The third row % column headings begins in row 2*ncol+1 of labels, etc. % % NOTE: Individual column headings must not contain blanks % % x = column vector of x values % y = matrix of y values. y has length(x) rows and ncols columns >> % read labels and x-y data >> [labels,month,t] = readColData(\'PDXtemperature.dat\',4,5); >> plot(month,t(:,1),\'ro\',month,t(:,2),\'c+\',month,t(:,3),\'g-\'); >> xlabel(labels(1,:)); % add axis labels and plot title >> ylabel(\'temperature (degrees F)\'); >> title(\'Monthly average temperature for Portland International Airport\'); >> % add a plot legend using labels read from the file >> legend(labels(2,:),labels(3,:),labels(4,:));