Combining functions and commands in to one script
15 views (last 30 days)
Show older comments
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
Rik
on 29 Oct 2020
In that case you can put all the code you need in a single file. What exactly is the issue you are having?
Accepted Answer
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
Walter Roberson
on 1 Nov 2020
The actual issue here is that when you have a function then as soon as the function returns, all local variables will be discarded. If you want the content of the variable outside of the function you should return it from the function and assign to a variable when you do.
drummer
on 1 Nov 2020
Edited: 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
More Answers (1)
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
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!