Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Matlab. Need help fixing code ASAP After each case, It should return to mypick=m

ID: 3663092 • Letter: M

Question

Matlab. Need help fixing code ASAP

After each case, It should return to mypick=menu...... it should only stop when the user decides to exit the program. And if the user chooses to plot data before loading data, there should be an error message, and then return to mypick=menu......

I need help fixing the code. Thanks!

mainscript.m

disp('Hi. What would you like to do?');
mypick=menu('What would you like to do?','Load data', 'Plot average day',...
'Plot daily values','Exit the program');

count=0;
switch mypick
case 1
disp('You have chosen to load a data.')
uiwait(msgbox({'You will be asked to enter' 'a month number.'...
'Here are your choices:' '1 = January' '2 = February'...
'3 = March' '4 = April' '5 = May' '6 = June', '7 = July'...
'8 = August' '9 = September' '10 = October' '11 = November'...
'12 = December'}));
prompt={'Enter Month Number'};
name = 'Month Number';
defaultans = {'1'};
options.Interpreter = 'tex';
answer = inputdlg(prompt,name,[1 40],defaultans,options);
mntNo=cell2mat(answer);
mntNo=str2double(mntNo);
[sunData, sunTimeMatrix]=loadSolarData(mntNo)
[windData, windTimeMatrix] = loadWindData(mntNo)
count=count+1;

  
case 2
if count==0;
errordlg('No data loaded','File Error');

else
disp('Plot average day')
plotAverageDay(sunData,sunTimeMatrix,windData,windTimeMatrix)

end
  
case 3
if count==0;
errordlg('No data loaded','File Error');
else
disp('Plot Daily Values')
plotDailyValues(sunData,sunTimeMatrix,windData,windTimeMatrix)
end


case 4
disp('Good Bye!') Exit script

  
otherwise
disp('You have closed the menu screen!')
end

Explanation / Answer

A simple plot of data from a file

This example show you how to load a simple data set and plot it.

The PDXprecip.dat file contains two columns of numbers. The first is the number of the month, and the second is the mean precipitation recorded at the Portland International Airport between 1961 and 1990. (For an abundance of weather data like this check out the Oregon Climate Service)

Here are the MATLAB commands to create a symbol plot with the data from PDXprecip.dat. These commands are also in the script file precipPlot.m for you to download.

Plotting data from files with column headings

If all your data is stored in files that contain no text labels, the load command is all you need. I like labels, however, because they allow me to document and forget about the contents of a file. To use the loadfor such a file I would have to delete the carefully written comments everytime I wanted to make a plot. Then, in order to minimize my effort, I might stop adding the comments to the data file in the first place. For us control freaks, that leads to an unacceptable increase in entropy of the universe! The solution is to find a way to have MATLAB read and deal with the text comments at the top of the file.

The following example presents a MATLAB function that can read columns of data from a file when the file has an arbitrary length text header and text headings for each columns.

The file has a five line header (including blank lines) and each column of numbers has a text label. To use this data with the load command you would have to delete the text labels and save the file. A better solution is to have MATLAB read the file without destroying the labels. Better yet, we should be able to tell MATLAB to read and use the column headings when it creates the plot legend.

There is no built-in MATLAB command to read this data, so we have to write an m-file to do the job. One solution is the file readColData.m. The full text of that function won't be reproduced here. You can click on the link to examine the code and save it on your computer if you like.

Here is the prologue to readColData.m

The first line of the file is the function definition. Following that are several lines of comment statements that form a prologue to the function. Because the first line after the function definition has a non-blank comment statement, typing ``help readColData'' at the MATLAB prompt will cause MATLAB to print the prologue in the command window. This is how the on-line help to all MATLAB functions is provided.

The prologue is organized into four sections. First is a brief statement of what the function does. Next is a synopsis of the ways in which the function can be called. Following that the input and output parameters are described.

Here are the MATLAB commands that use readColData.m to plot the data in PDXtemperature.dat. The commands are also in the script file multicolPlot.m

The MATLAB load Command

There is more than one way to read data into MATLAB from a file. The simplest, though least flexible, procedure is to use the load command to read the entire contents of the file in a single step. The loadcommand requires that the data in the file be organized into a rectangular array. No column titles are permitted. One useful form of the load command is

where ``name.ext'' is the name of the file containing the data. The result of this operation is that the data in ``name.ext'' is stored in the MATLAB matrix variable called name. The ``ext'' string is any three character extension, typically ``dat''. Any extension except ``mat'' indicates to MATLAB that the data is stored as plain ASCII text. A ``mat'' extension is reserved for MATLAB matrix files (see ``help load'' for more information).

Suppose you had a simple ASCII file named my_xy.dat that contained two columns of numbers. The following MATLAB statements will load this data into the matrix ``my_xy'', and then copy it into two vectors, x and y.

You don't need to copy the data into x and y, of course. Whenever the ``x'' data is needed you could refer to it as my_xy(:,1). Copying the data into ``x'' and ``y'' makes the code easier to read, and is more aesthetically appealing. The duplication of the data will not tax MATLAB's memory for most modest data sets.