Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 9.11 KB | None | 0 0
  1. %projectStatCalc takes in a file, output path, output file name, and a
  2. %username and calculates statistical data contained in the file.
  3.  
  4. % By submitting this assignment, I agree to the following:
  5. %  “Aggies do not lie, cheat, or steal, or tolerate those who do”
  6. %  “I have not given or received any unauthorized aid on this assignment”
  7. %
  8. % Name:         BRETT BOYER
  9. % Section:      531
  10. % Team:         14
  11. % Assignment:   MATLAB PROJECT
  12. % Date:         23 APRIL 2017
  13.  
  14. function [] = projectStatCalc(file,outputPath,outputFile,user)
  15.  
  16. %Loads data, establishes output file path, puts header in output
  17. %document, and gets the number of columns in the data.
  18. data = load(file);
  19. myPath = strcat(outputPath,'\',outputFile);
  20. fid = fopen(myPath,'wt+');
  21. fprintf(fid,'File generated by %s on %s\n',user,datetime);
  22. [~,len] = size(data);
  23.  
  24. %Checks if there is more than one column, if there is, it informs the user
  25. %there needs to only one column and then closes, otherwise it renames data.
  26. if(len>1)
  27.     errordlg('Data file contains more than 1 column.','Input File Error');
  28.     fclose(fid);
  29.     return
  30. else
  31.     newData=data;
  32. end
  33.  
  34. %Plots a histfit and a normplot in a single window and sets the starting
  35. %size and position to help with ease of viewing.
  36. wib = figure('Name','Plots');
  37. set(wib,'Position',[750,250,600,600]);
  38. subplot(2,2,[1,2]);
  39. histfit(newData);
  40. subplot(2,2,[3,4]);
  41. normplot(newData);
  42.  
  43. %Asks the user if the data follows a normal distrobution, if it does, it
  44. %calculates the data and moves on through the code. If it isn't, then it
  45. %logs it in the output file and closes the program.
  46. choice = questdlg('Is the data normally disributed?','Verification','Yes','No','No');
  47. switch choice
  48.     case 'Yes'
  49.         close(wib);
  50.         fprintf(fid,'\nData:\n');
  51.         avg=mean(newData);
  52.         fprintf(fid,'Mean   = %06.2f\n',avg);
  53.         medi=median(newData);
  54.         fprintf(fid,'Median = %06.2f\n',medi);
  55.         mde=mode(newData);
  56.         fprintf(fid,'Mode   = %06.2f\n',mde);
  57.         vari=var(newData);
  58.         fprintf(fid,'Var    = %06.2f\n',vari);
  59.         Stdev=std(newData,1);
  60.         fprintf(fid,'Stdev  = %06.2f *Used Population Formula\n',Stdev);
  61.         if(numel(newData)<=30)
  62.             Stdev=std(newData,0);
  63.             fprintf(fid,'Stdev  = %06.2f *Used Sample Formula\n',Stdev);
  64.         end
  65.         smol=min(newData);
  66.         fprintf(fid,'Min    = %06.2f\n',smol);
  67.         bigg=max(newData);
  68.         fprintf(fid,'Max    = %06.2f\n',bigg);
  69.         count=numel(newData);
  70.         fprintf(fid,'Count  = %06.0f\n',count);
  71.        
  72.         %Asks if the user wants to perform z-table calculations. If they
  73.         %do, then it asks for a number of calculations and checks if their
  74.         %input is a number. If they dont, it closes the program.
  75.         zpick = questdlg('Would you like to perform z-table calculations on the data?','Confirmation','Yes','No ','No ');
  76.         switch zpick
  77.             case 'Yes'
  78.                 fprintf(fid,'\nZ-Table Calculations performed by %s:',user);
  79.                 n = inputdlg('Enter number of z-table calculations you want to do:','Input',1,{'1'});
  80.                 if(isnan(str2double(n)))
  81.                     n = inputdlg('Not a number. Please enter the number of z-table calculations you want to do:','Input',1,{'1'});
  82.                 end
  83.             otherwise
  84.                 fclose(fid);
  85.                 return
  86.         end
  87.        
  88.         %Prompts the user about which region is of interest (e.g. above or
  89.         %below a specific value, or in between two values). It then asks for
  90.         %the value (or values in the case of in between), and if the
  91.         %value(s) is (are) x value(s) from the distrobution or z-score(s),
  92.         %and then does the calculation the user specified.
  93.        
  94.         %If at any point during this process the user cancels out of a text
  95.         %box the program terminates.
  96.         for h=1:1:round(str2double(n))
  97.             switch zpick
  98.                 case 'Yes'
  99.                     aoi=questdlg('Which area of the distrobution with respect to a value is of interest?','Confirmation','Above','Below','In Between','In Between');
  100.                     switch aoi
  101.                         case 'Above'
  102.                             x = inputdlg('Enter the value:','Input',1,{'15'});
  103.                             x=str2double(x);
  104.                             xorz=questdlg('Is this a value in the distrobution or a z-score?','Confirmation','X Value','Z-Score','X Value');
  105.                             switch xorz
  106.                                 case 'X Value'
  107.                                     res = 1-(normcdf(x,avg,Stdev));
  108.                                     fprintf(fid,'\nThe probability for values above %.2f is %.2f\n',x,res);
  109.                                 case 'Z-Score'
  110.                                     x=(x*Stdev)+avg;
  111.                                     res = 1-(normcdf(x,avg,Stdev));
  112.                                     fprintf(fid,'\nThe probability for values above %.2f is %.2f\n',x,res);
  113.                                 otherwise
  114.                                     fclose(fid);
  115.                                     return;
  116.                             end
  117.                         case 'Below'
  118.                             x = inputdlg('Enter the value:','Input',1,{'10'});
  119.                             x=str2double(x);
  120.                             xorz=questdlg('Is this a value in the distrobution or a z-score?','Confirmation','X Value','Z-Score','X Value');
  121.                             switch xorz
  122.                                 case 'X Value'
  123.                                     res = normcdf(x,avg,Stdev);
  124.                                     fprintf(fid,'\nThe probability for values below %.2f is %.2f\n',x,res);
  125.                                 case 'Z-Score'
  126.                                     x=(x*Stdev)+avg;
  127.                                     res = normcdf(x,avg,Stdev);
  128.                                     fprintf(fid,'\nThe probability for values below %.2f is %.2f\n',x,res);
  129.                                 otherwise
  130.                                     fclose(fid);
  131.                                     return;
  132.                             end
  133.                         case 'In Between'
  134.                             x = inputdlg({'Enter upper value:','Enter lower value:'},'Inputs',1,{'15','10'});
  135.                             S = sprintf('%s*', x{:});
  136.                             N = sscanf(S, '%f*');
  137.                             xorz=questdlg('Are these values in the distrobution or z-scores?','Confirmation','X Values','Z-Scores','X Values');
  138.                             switch xorz
  139.                                 case 'X Values'
  140.                                     res = normcdf(N(1),avg,Stdev)-normcdf(N(2),avg,Stdev);
  141.                                     fprintf(fid,'\nThe probability for values between %.2f and %.2f is %.2f\n',N(2),N(1),res);
  142.                                 case 'Z-Scores'
  143.                                     N(1)=(N(1)*Stdev)+avg;
  144.                                     N(2)=(N(2)*Stdev)+avg;
  145.                                     res = normcdf(N(1),avg,Stdev)-normcdf(N(2),avg,Stdev);
  146.                                     fprintf(fid,'\nThe probability for values between %.2f and %.2f is %.2f\n',N(2),N(1),res);
  147.                                 otherwise
  148.                                     fclose(fid);
  149.                                     return;
  150.                             end
  151.                         otherwise
  152.                             fclose(fid);
  153.                             return
  154.                     end
  155.                 otherwise
  156.                     fclose(fid);
  157.                     return
  158.             end
  159.         end
  160.        
  161.         %Takes a user input of a probability from 0 to 1 and returns the x
  162.         %value and z-score associated with it.
  163.        
  164.         %***Note that this uses the icdf() function, which is exclusive to
  165.         %the statistics package for matlab.***
  166.         probPick = questdlg('Would you like to perform a probability value search in the data?','Confirmation','Yes','No ','No ');
  167.         switch probPick
  168.             case 'Yes'
  169.                 o = inputdlg('Enter number of value searches you want to do:','Input',1,{'1'});
  170.                 for d=1:str2double(o)
  171.                     p = inputdlg('Enter the probability:','Input',1,{'0.5'});
  172.                     if(str2double(p)>1 || str2double(p)<0)
  173.                         p = inputdlg('Enter the probability (between 0 and 1):','Input',1,{'0.5'});
  174.                     end
  175.                     pd = makedist('Normal',avg,Stdev);
  176.                     x = icdf(pd,str2double(p));
  177.                     z=(x-avg)/Stdev;
  178.                     fprintf(fid,'\nThe z-score and x value that correspond to the probability %.2f are %.2f and %.2f',str2double(p),x,z);
  179.                 end
  180.             otherwise
  181.                 fclose(fid);
  182.                 return
  183.         end
  184.         msgbox('Output has been written to your file path.');
  185.         fclose(fid);
  186.         return
  187.     otherwise
  188.         close(wib);
  189.         fprintf(fid,'\nUser entered data that did not follow a normal distrobution.');
  190.         fclose(fid);
  191.         return
  192. end
  193. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement