Combining functions and commands in to one script

14 次查看(过去 30 天)
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 个评论
Rik
Rik 2020-10-29
In that case you can put all the code you need in a single file. What exactly is the issue you are having?
Sunshine
Sunshine 2020-10-30
I wasn't sure how to go about connecting all the info. So I was asking for some guidance. I'm trying your suggestion.

请先登录,再进行评论。

采纳的回答

drummer
drummer 2020-11-1
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 个评论
Walter Roberson
Walter Roberson 2020-11-1
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
drummer 2020-11-1
编辑:drummer 2020-11-1
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

请先登录,再进行评论。

更多回答(1 个)

drummer
drummer 2020-10-30
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 个评论
Sunshine
Sunshine 2020-11-7
Thanks for the feedback. I was playing with java.lang.Thread.sleep(duration*1000) % in milisec
I get the following error: No method 'java.lang.Thread.sleep' with matching signature found...
using the below code. I haven't found any docs to say what this means or how to fix it. Any ideas?
fprintf('Wanna choose files?\n');
prompt = ('y or n ')
str = input(prompt, 's');
if str == 'y'
fprintf('choose x file\n');
[fileX,path] = uigetfile('yourExtension');
ReadInFileX = load(fileX)
java.lang.Thread.sleep(duration*3000) % in milisec
%[fileX, path] = uigetfile('yourExtension');
%if isequal(fileX,0)
% disp('User selection for X file cancelled');
%end
% go through your code
fprintf('choose y file\n');
[fileY,path] = uigetfile('*.csv');
ReadInFileY = load(fileY)
java.lang.Thread.sleep(duration*3000) % in milisec
%if isequal(fileY,0)
% disp('User selection for Y file cancelled');
%end
%[fileY, path] = uigetfile('yourExtension');
else
fprintf('back off.')
end
Sunshine
Sunshine 2020-11-7
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by