Basic MATLAB Programming Problem I am new to the MATLAB lang

Basic MATLAB Programming Problem. I am new to the MATLAB language so please use write in basic code. Please include step by step comments, explaining all processes and functions. Thank you in advance.

1. Temperature Converter (33 points) Write a function that displays a menu that allows the user to do the following (11): Convert a temperature from Celsius to Fahrenheit using the formula F C 32 (2): Convert a temperature from Fahrenheit to Celsius using the formula C (F-32 (3): Quit the program. Read the choice from the user. If the user chose 1 or 2, first call a subroutine to get a temperature from the user. Then, pass the temperature as a parameter to the appropriate function and return the converted temperature. Output the returned converted temperature, and then display the menu once agai If the user chose 3, quit the program. A sample run is included below: RESTART Fahrenheit to Celsius Celsius to Fahrenheit 3. Quit Please select one of the above Please enter a Fahrenheit temperature: 212 212.00 Fahrenheit degrees i 100.00 Celsius degrees Fahrenheit to Celsius Celsius to Fahrenhe 3. Quit Please select one of the above Please enter a Celsius temperature 0.00 Celsius degrees is 2.00 Fahrenheit degrees. Fahrenheit to Celsius Celsius to Fahrenheit 3. Quit Please select one of the above Please enter a valid choice Fahrenheit to Celsius Celsius to Fahrenheit 3. Quit Please select one of the above 2. Writing a Random Temperature File (33 points) Write a function that writes a series of random Fahrenheit temperatures and their correspond. ing Celsius temperatures to a tab-delimited file. Use 32 to 212 as your temperature range. From the user, obtain the following: The number of temperatures to randomly generate. The name of the output file. A sample run is included below (you must follow the format provided below) Please enter the name of your f Example txt Please enter the number of iterations: 100 Example txt Fahrenheit Celsius 152.00 66.67 97.00 36.11 32.000.00 99.00 37.22 35.00 1.67 etc 3. Reading a Random Temperature File (34 points) Write a function that reads a file produced by Problem 2. Focusing only on the Celsius temperatures in the file, calculate and display to screen the following: Mean or Average Minimum Maximum

Solution

Answer 1

% \'disp\' is used to display on console.
% This is how one can declare a variable in Matlab. This also shows how powerful MATLAB is. You need not to specify the data type.
flag = true;
% This is how a while is declared. The end is marked by keyword \'end\'. The loop ends when a user enters 3.
while(flag)
disp(\'1. Fahrenheit to Celsius\');
disp(\'2. Celsius to Fahrenheit\');
disp(\'3. Quit\');
disp(\'Please select one of the above:\')
val = input(\'\')
switch val
% Convert Fahrenheit to Celsius
case 1
Faren=input(\'Please enter a Fahrenheit temperature: \');
% num2Str is a utility of matlab whoch converts number/decimal to string. \'%4.2f\' is used to format the data. Here we want to display till 2 digits after decimal.
disp([ num2str(Faren, \'%4.2f\') \' Fahrenheit degrees is \' num2str(((Faren-32)/1.8),\'%4.2f\') \' Celcius degrees\' ]);
% Convert Celsius to Fahrenheit   
case 2
Celsius=input(\'Please enter a Celsius temperature: \');
disp([ num2str(Celsius, \'%4.2f\') \' Celcius degrees is \' num2str((Celsius*1.8+32),\'%4.2f\') \' Fahrenheit degrees\']);
% Condition to exit the program and loop
case 3
flag=false;
end
end

Answer 2

% \'disp\' is used to display on console.
% This is how one can declare a variable in Matlab. This also shows how powerful MATLAB is. You need not to specify the data type.
fileName = input(\'Please enter the name of your file: \', \'s\');
noOfIter = input(\'Please enter the number of iterations: \');
% The temperature range as specified in question.
minTempRange = 32;
maxTempRange = 212;
% headers for the file
header1 = \'Fahrenheit\';
header2 = \'Celcius\';
% Creating a file with the entered name. \'w\' is used to create and write to the file.
fileID = fopen(fileName,\'w\');
fprintf(fileID, [ header1 \' \' header2 \'\ \']);
%fprintf(fid, \'%f %f \ \', [A B]\');
while(noOfIter > 0)
% generating a random number between specified range. This will be our Fahrenheit temperature.
randomTemp = (maxTempRange-minTempRange).*rand(1,1) + minTempRange;
% num2Str is a utility of matlab whoch converts number/decimal to string. \'%4.2f\' is used to format the data. Here we want to display till 2 digits after decimal.
fprintf(fileID, \'%f %f \ \', [num2str(randomTemp, \'%4.2f\') num2str(((randomTemp-32)/1.8),\'%4.2f\')]\');
disp([ num2str(randomTemp, \'%4.2f\') \' Fahrenheit degrees is \' num2str(((randomTemp-32)/1.8),\'%4.2f\') \' Celcius degrees\' ]);
noOfIter--;
end
fclose(fileID);

%File opeartions are same as in other programming languages you might be aware of.

Answer 3

fid = fopen(\'myfile.txt\')
% Textscan reads the data from second coloumn \'%*s %f %*[^\ ]\'. It is cell array.Generally it is used to hold multi-dimesional arrays.
data = textscan(fid, \'%*s %f %*[^\ ]\',\'Celsius\',1);
disp(\'Average=\'mean(cell2mat(data)));
disp(\'Minimum=\'min(cell2mat(data)));
disp(\'Maximum=\'max(cell2mat(data)));
fid = fclose(fid);

Basic MATLAB Programming Problem. I am new to the MATLAB language so please use write in basic code. Please include step by step comments, explaining all proces
Basic MATLAB Programming Problem. I am new to the MATLAB language so please use write in basic code. Please include step by step comments, explaining all proces

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site