Asked by Clifford Shelton
on 1 Jul 2012

I have written three different functions, each with different m.file names. Each of the functions have prompted inputs in the command window.

Each of the functions are really a group of calculations that I do in succession.

How would i be able to put all three functions into one program where I can be prompted in succession in the command window for each input?

I'm trying to find a more efficient way to do these calculations. Currently I'm manually typing in everything over and over and over and over.

Help most appreciated!

[Merged information from duplicate Question]

I am doing a regression and forecast of a baseball team's scores. To do this..I've broken the process up into three steps which I have coded as three different functions.

1.) Function called "importhome.m" which imports the data set. the function has an input prompt for the name of the team.

2.) Function called "preforecast2" does a a preliminary calculation. It has no input prompts.

3.) Function called "forecast2" which does the final calculation and generates a graph with a forecast. It has a input prompt for lengths of the graph and forecast.

Doing each function one by one has become cumbersome. So I want to put the whole process into one program. But I'm clueless as to how to put functions with input prompts and embedded variables into one program. PLEASE HELP!

**For clearer understanding I'm going to include the whole code:
(I appreciate all the help to answer my questions before, but without the posted code I've gotten very general responses...which I am clueless as to how to apply)**

**First Function:**

function importhome(fileToRead1) %IMPORTFILE(FILETOREAD1) % Imports data from the specified file % FILETOREAD1: file to read

% Auto-generated by MATLAB on 05-May-2012 23:12:52

% Import the file sheetName='Sheet1'; [numbers, strings, raw] = xlsread(fileToRead1, sheetName); if ~isempty(numbers) newData1.data = numbers; end

if ~isempty(strings) && ~isempty(numbers) [strRows, strCols] = size(strings); [numRows, numCols] = size(numbers); likelyRow = size(raw,1) - numRows; % Break the data up into a new structure with one field per column. if strCols == numCols && likelyRow > 0 && strRows >= likelyRow newData1.colheaders = strings(likelyRow, :); end end

% Create new variables in the base workspace from those fields. for i = 1:size(newData1.colheaders, 2) assignin('base', genvarname(newData1.colheaders{i}), newData1.data(:,i)); end

**Second Function:**

%Since I was unable to include this section of calculations with in the %"forecast2" script I've done it stand alone here. %This calculation is to use the fft spectral analysis function within %Matlab %process in one program! FourierScore = fft(Score); FourierScore(1)=[]; A = length(FourierScore); powerScore = abs(FourierScore(1:floor(A/2))).^2; powerScore = powerScore(:); nyquist = 1/2; freqScore = (1:A/2)/(A/2)*nyquist; freqScore = freqScore(:);

FourierAllow = fft(Allow); FourierAllow(1)=[]; B = length(Allow); powerAllow = abs(FourierAllow(1:floor(B/2))).^2; powerAllow = powerAllow(:); freqAllow = (1:B/2)/(B/2)*nyquist; freqAllow = freqAllow(:);

**Third Function:**

function forecast2(Size, Scorecycle, Allowcycle,Forecast) %CREATEFIGURE(X1,X2,X3) % X1: vector of x data % Y1: vector of y data % Y2: vector of y data % Y3: vector of y data % Y4: vector of y data

% These commands must be executed from inside your function evalin('base','save myvars.mat'); load myvars.mat % set variables figure Size = Size; Forecast = Forecast; Scorecycles = Scorecycle; Allowcycles = Allowcycle;

FourierScore = fft(Score); FourierScore(1)=[]; A = length(FourierScore); powerScore = abs(FourierScore(1:floor(B/2))).^2; powerScore = powerScore(:); nyquist = 1/2; freqScore = (1:A/2)/(A/2)*nyquist; freqScore = freqScore(:);

FourierAllow = fft(Allow); FourierAllow(1)=[]; B = length(Allow); powerAllow = abs(FourierAllow(1:floor(B/2))).^2; powerAllow = powerAllow(:); freqAllow = (1:B/2)/(B/2)*nyquist; freqAllow = freqAllow(:);

%Set other variables imported with the data yScore = Score(:); yAllow = Allow(:); %yHit = H(:);

n = Size; t = (1:Size)'; tt = (Size:Forecast); %nn = Size+10; games = 1:Size;

%The Runs Scored Regression

Scoredata(1:4) = struct('XScore',NaN(Size,3),'bhatScore',NaN(3,1),'yhatScore',NaN,'yhatScorePred',NaN);

for ii = 1:Scorecycles tmpScore = 2*pi*(freqScore(ii))*t; tmpScore2 = 2*pi*(freqScore(ii))*(tt)'; Scoredata(ii).XScore = rand(Size,3); Scoredata(ii).XScore(:,2) = cos(tmpScore)'; Scoredata(ii).XScore(:,3) = sin(tmpScore)'; Scoredata(ii).bhatScore = Scoredata(ii).XScore\yScore; Scoredata(ii).yhatScore = Scoredata(ii).bhatScore(1)+Scoredata(ii).bhatScore(2)*cos(tmpScore)+Scoredata(ii).bhatScore(3)*sin(tmpScore); Scoredata(ii).yhatScorePred = Scoredata(ii).bhatScore(1)+Scoredata(ii).bhatScore(2)*cos(tmpScore2)+Scoredata(ii).bhatScore(3)*sin(tmpScore2);

yhatScore = [Scoredata.yhatScore]; yhatScoreM = sum(horzcat(Scoredata.yhatScore),2) ./Scorecycles; yhatScoreMPred = sum(horzcat(Scoredata.yhatScorePred),2) ./Scorecycles; yhatScoreForecast = [yhatScoreM; yhatScoreMPred];

%The Runs Against Regression

yAllow = Allow(:); Allowdata(1:4) = struct('XAllow',NaN(Size,3),'bhatAllow',NaN(3,1),'yhatAllow',NaN,'yhatAllowPred',NaN);

for xx = 1:Allowcycles tmpAllow = 2*pi*(freqAllow(xx))*t; tmpAllow2 = 2*pi*(freqAllow(xx))*(tt)'; Allowdata(xx).XAllow = rand(Size,3); Allowdata(xx).XAllow(:,2) = cos(tmpAllow)'; Allowdata(xx).XAllow(:,3) = sin(tmpAllow)'; Allowdata(xx).bhatAllow = Allowdata(xx).XAllow\yAllow; Allowdata(xx).yhatAllow = Allowdata(xx).bhatAllow(1)+Allowdata(xx).bhatAllow(2)*cos(tmpAllow)+Allowdata(xx).bhatAllow(3)*sin(tmpAllow); Allowdata(xx).yhatAllowPred = Allowdata(xx).bhatAllow(1)+Allowdata(xx).bhatAllow(2)*cos((tmpAllow2))+Allowdata(xx).bhatAllow(3)*sin((tmpAllow2));

yhatAllow = [Allowdata.yhatAllow]; yhatAllowM = sum(horzcat(Allowdata.yhatAllow),2) ./Allowcycles; yhatAllowMPred = sum(horzcat(Allowdata.yhatAllowPred),2) ./Allowcycles; yhatAllowForecast = [yhatAllowM; yhatAllowMPred];

subplot (2,1,1), plotyy(t,yScore,1:Forecast+1,yhatScoreForecast) title('Boston July 4rdt 2012');

subplot (2,1,2), plotyy(t,yAllow,1:Forecast+1,yhatAllowForecast)

Sorry for the extensive code. But my previous answers I wasn't able to use. PLEASE HELP!! I'm trying to get all three fucntions into one program with all the inputs. *Your help is most appreciated. *

*No products are associated with this question.*

Answer by Image Analyst
on 1 Jul 2012

Just add them to the same file. The only requirement is that the main code must start with the word "function" - it can't be a script anymore (scripts don't use the word function, they're just a bunch of lines of code. The side effect is that no other m-files can see those once you've embedded them into your main program, but if that's not a problem then go for it.

Show 2 older comments

Clifford Shelton
on 4 Jul 2012

I'm an xtreme beginner. could you give a code example of exactly how that would be done? I have posted a question with a similar title...but this one displays the full code I am trying to synthesize into one program.

What you are saying makes total sense..I am just too much of a beginner to actually know how to write the code and utiilze what you are saying.

Your help is SOOOOO appreciated!

Image Analyst
on 4 Jul 2012

Didn't you write the 3 m-files? Basically you need to do File->New->Script from the MATLAB menu. Then you need to type these lines into it.

[fileToRead1, folder] = uigetfile(); fullFileName = fullfile(folder, fileToRead1); % Call the first function. importhome(fullFileName);

% Now call the second script second_script(); % Or whatever the name of the m-file is.

% First assign Size, Scorecycle, Allowcycle,Forecast % Then run the third function. forecast2(Size, Scorecycle, Allowcycle,Forecast)

When you run it, MATLAB will ask you to save the file. Call it main_script.m or something similar.

Finally, read the Getting Started part of the help. This is crucial if you don't even know how to create a script and run it.

Clifford Shelton
on 5 Jul 2012

Thanks Image Analyst. Yes I wrote the original M-files..but as you can see, I have a ton of holes in my knowledge of programming. this whole "uigetfile" stuff is new to me!

This has been very helpful. THANKS!

Answer by Rohan Kale
on 2 Jul 2012

if the functions are just based on some formulae , i mean for example it may be f(x)= some trigonometric formulae/expressions , you can use functions that use handlers as in f=@(x)(sin(x)./x) [ just for example] or may be more than one variable dependent f=@(x,y,z) (x.^2 + y.^3 ....) . these will be the function definitions and for invoking them just use f(2) or f(3,4) or f(a) where 'a' may be your variable ,and all these under one m file.

Clifford Shelton
on 4 Jul 2012

thanks for the help. But I'm not sure how to apply this to my code. Im going to post my code in a separate question so it can be easy to see.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments