MATLAB Answers

Combining functions and commands in to one script

10 views (last 30 days)
Sunshine
Sunshine on 29 Oct 2020
Commented: Sunshine on 7 Nov 2020
I have to provide a script that reads in values from 2 separate files. The script should also provide an output file for the predicted value, given the input data. So I have created 2 functions to read in the input files: flle1 and file2. I then have commands that I ran to combine the info in file1 and file2 so that the combined info from file1 and file2 can be input in to the nftool. I also have commands that I ran to modify file3 in order to use it as target data in the nftool. How do I combine this info (functions, commands, nftool data) in to one script so that I can then run this script such that another file1 and file2 can be provided to give an output file?
  4 Comments
Sunshine
Sunshine on 30 Oct 2020
I wasn't sure how to go about connecting all the info. So I was asking for some guidance. I'm trying your suggestion.

Sign in to comment.

Accepted Answer

drummer
drummer on 1 Nov 2020
Functions by default don't show their scope variables in the workspace.
Did you have any error while running your code? If you don't, you can just perform a test by not using function in your 1st line.
So, just comment the first line of your code, and check if despx and despy appears in your workspace.
Then, your're performing the pipeline I mentioned before.
% Procedural code
% Here you call your functions
despx = importXfile('yourInput.csv')
despy = importYfile('youInput.csv')
% -------------------
%% Functions section
function x_2 = importXfile(filename, dataLines)
% function code
function y_2 = importYfile(filename, dataLines)
% function code
  3 Comments
drummer
drummer on 1 Nov 2020
Great.
If in the near future you want to check the scope of variables of your functions while running, you can click in the line number so it gets a red square: that's debugging stop. Your code will stop in this line, and all the variables you're creating in the environment of your function will appear in the workspace.
If you have a loop in your function for example, and you create a debug stop in the end of the loop, you can check the behavior of every related variables within the function.
If that helped, please accept the answer. =)
Good coding.
Cheers

Sign in to comment.

More Answers (1)

drummer
drummer on 30 Oct 2020
You can write your pipeline as a regular script, calling your input files, processing, and obtaining your result.
As you're up-to-date with MATLAB, you can write your functions in the end of the script.
% Write your pipeline
myInputFile = myOwnReadFunction(whateverInputArguments);
% Process your data
myResults = processingData(myInputFile);
%% Below, write your functions, in the end of your script:
function data = myOwnReadFunction(input1, input2)
% code what you need
end
function result = processingData(input1)
% code your calculation
end
Cheers
  11 Comments
Sunshine
Sunshine on 7 Nov 2020
After continued reading of docs, I think I figured it out. Or at least I'm not getting the error anymore. I set duration. Ha....thanks.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!