Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %projectStatCalc takes in a file, output path, output file name, and a
- %username and calculates statistical data contained in the file.
- % By submitting this assignment, I agree to the following:
- % “Aggies do not lie, cheat, or steal, or tolerate those who do”
- % “I have not given or received any unauthorized aid on this assignment”
- %
- % Name: BRETT BOYER
- % Section: 531
- % Team: 14
- % Assignment: MATLAB PROJECT
- % Date: 23 APRIL 2017
- function [] = projectStatCalc(file,outputPath,outputFile,user)
- %Loads data, establishes output file path, puts header in output
- %document, and gets the number of columns in the data.
- data = load(file);
- myPath = strcat(outputPath,'\',outputFile);
- fid = fopen(myPath,'wt+');
- fprintf(fid,'File generated by %s on %s\n',user,datetime);
- [~,len] = size(data);
- %Checks if there is more than one column, if there is, it informs the user
- %there needs to only one column and then closes, otherwise it renames data.
- if(len>1)
- errordlg('Data file contains more than 1 column.','Input File Error');
- fclose(fid);
- return
- else
- newData=data;
- end
- %Plots a histfit and a normplot in a single window and sets the starting
- %size and position to help with ease of viewing.
- wib = figure('Name','Plots');
- set(wib,'Position',[750,250,600,600]);
- subplot(2,2,[1,2]);
- histfit(newData);
- subplot(2,2,[3,4]);
- normplot(newData);
- %Asks the user if the data follows a normal distrobution, if it does, it
- %calculates the data and moves on through the code. If it isn't, then it
- %logs it in the output file and closes the program.
- choice = questdlg('Is the data normally disributed?','Verification','Yes','No','No');
- switch choice
- case 'Yes'
- close(wib);
- fprintf(fid,'\nData:\n');
- avg=mean(newData);
- fprintf(fid,'Mean = %06.2f\n',avg);
- medi=median(newData);
- fprintf(fid,'Median = %06.2f\n',medi);
- mde=mode(newData);
- fprintf(fid,'Mode = %06.2f\n',mde);
- vari=var(newData);
- fprintf(fid,'Var = %06.2f\n',vari);
- Stdev=std(newData,1);
- fprintf(fid,'Stdev = %06.2f *Used Population Formula\n',Stdev);
- if(numel(newData)<=30)
- Stdev=std(newData,0);
- fprintf(fid,'Stdev = %06.2f *Used Sample Formula\n',Stdev);
- end
- smol=min(newData);
- fprintf(fid,'Min = %06.2f\n',smol);
- bigg=max(newData);
- fprintf(fid,'Max = %06.2f\n',bigg);
- count=numel(newData);
- fprintf(fid,'Count = %06.0f\n',count);
- %Asks if the user wants to perform z-table calculations. If they
- %do, then it asks for a number of calculations and checks if their
- %input is a number. If they dont, it closes the program.
- zpick = questdlg('Would you like to perform z-table calculations on the data?','Confirmation','Yes','No ','No ');
- switch zpick
- case 'Yes'
- fprintf(fid,'\nZ-Table Calculations performed by %s:',user);
- n = inputdlg('Enter number of z-table calculations you want to do:','Input',1,{'1'});
- if(isnan(str2double(n)))
- n = inputdlg('Not a number. Please enter the number of z-table calculations you want to do:','Input',1,{'1'});
- end
- otherwise
- fclose(fid);
- return
- end
- %Prompts the user about which region is of interest (e.g. above or
- %below a specific value, or in between two values). It then asks for
- %the value (or values in the case of in between), and if the
- %value(s) is (are) x value(s) from the distrobution or z-score(s),
- %and then does the calculation the user specified.
- %If at any point during this process the user cancels out of a text
- %box the program terminates.
- for h=1:1:round(str2double(n))
- switch zpick
- case 'Yes'
- aoi=questdlg('Which area of the distrobution with respect to a value is of interest?','Confirmation','Above','Below','In Between','In Between');
- switch aoi
- case 'Above'
- x = inputdlg('Enter the value:','Input',1,{'15'});
- x=str2double(x);
- xorz=questdlg('Is this a value in the distrobution or a z-score?','Confirmation','X Value','Z-Score','X Value');
- switch xorz
- case 'X Value'
- res = 1-(normcdf(x,avg,Stdev));
- fprintf(fid,'\nThe probability for values above %.2f is %.2f\n',x,res);
- case 'Z-Score'
- x=(x*Stdev)+avg;
- res = 1-(normcdf(x,avg,Stdev));
- fprintf(fid,'\nThe probability for values above %.2f is %.2f\n',x,res);
- otherwise
- fclose(fid);
- return;
- end
- case 'Below'
- x = inputdlg('Enter the value:','Input',1,{'10'});
- x=str2double(x);
- xorz=questdlg('Is this a value in the distrobution or a z-score?','Confirmation','X Value','Z-Score','X Value');
- switch xorz
- case 'X Value'
- res = normcdf(x,avg,Stdev);
- fprintf(fid,'\nThe probability for values below %.2f is %.2f\n',x,res);
- case 'Z-Score'
- x=(x*Stdev)+avg;
- res = normcdf(x,avg,Stdev);
- fprintf(fid,'\nThe probability for values below %.2f is %.2f\n',x,res);
- otherwise
- fclose(fid);
- return;
- end
- case 'In Between'
- x = inputdlg({'Enter upper value:','Enter lower value:'},'Inputs',1,{'15','10'});
- S = sprintf('%s*', x{:});
- N = sscanf(S, '%f*');
- xorz=questdlg('Are these values in the distrobution or z-scores?','Confirmation','X Values','Z-Scores','X Values');
- switch xorz
- case 'X Values'
- res = normcdf(N(1),avg,Stdev)-normcdf(N(2),avg,Stdev);
- fprintf(fid,'\nThe probability for values between %.2f and %.2f is %.2f\n',N(2),N(1),res);
- case 'Z-Scores'
- N(1)=(N(1)*Stdev)+avg;
- N(2)=(N(2)*Stdev)+avg;
- res = normcdf(N(1),avg,Stdev)-normcdf(N(2),avg,Stdev);
- fprintf(fid,'\nThe probability for values between %.2f and %.2f is %.2f\n',N(2),N(1),res);
- otherwise
- fclose(fid);
- return;
- end
- otherwise
- fclose(fid);
- return
- end
- otherwise
- fclose(fid);
- return
- end
- end
- %Takes a user input of a probability from 0 to 1 and returns the x
- %value and z-score associated with it.
- %***Note that this uses the icdf() function, which is exclusive to
- %the statistics package for matlab.***
- probPick = questdlg('Would you like to perform a probability value search in the data?','Confirmation','Yes','No ','No ');
- switch probPick
- case 'Yes'
- o = inputdlg('Enter number of value searches you want to do:','Input',1,{'1'});
- for d=1:str2double(o)
- p = inputdlg('Enter the probability:','Input',1,{'0.5'});
- if(str2double(p)>1 || str2double(p)<0)
- p = inputdlg('Enter the probability (between 0 and 1):','Input',1,{'0.5'});
- end
- pd = makedist('Normal',avg,Stdev);
- x = icdf(pd,str2double(p));
- z=(x-avg)/Stdev;
- fprintf(fid,'\nThe z-score and x value that correspond to the probability %.2f are %.2f and %.2f',str2double(p),x,z);
- end
- otherwise
- fclose(fid);
- return
- end
- msgbox('Output has been written to your file path.');
- fclose(fid);
- return
- otherwise
- close(wib);
- fprintf(fid,'\nUser entered data that did not follow a normal distrobution.');
- fclose(fid);
- return
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement