Combining functions and commands in to one script

12 visualizaciones (últimos 30 días)
Sunshine
Sunshine el 29 de Oct. de 2020
Comentada: Sunshine el 7 de Nov. de 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 comentarios
Rik
Rik el 29 de Oct. de 2020
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 el 30 de Oct. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

drummer
drummer el 1 de Nov. de 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 comentarios
Walter Roberson
Walter Roberson el 1 de Nov. de 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
drummer el 1 de Nov. de 2020
Editada: drummer el 1 de Nov. de 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

Iniciar sesión para comentar.

Más respuestas (1)

drummer
drummer el 30 de Oct. de 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 comentarios
Sunshine
Sunshine el 7 de Nov. de 2020
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 el 7 de Nov. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by