How can i call function without any output arguments?

On my project my code is working. After that i tried to add a funciton on my code. As you can see down below i put my codes in a funciton & called it "plotter" without any output arguments.
function plotter(files, dir_path)
Exceldir = dir_path + "\ExcelFiles";
cd(Exceldir); % Change current directory to the where excel files restored in.
for i=1:length(files)
% Get sheet names being in the excel file.
sheets = sheetnames(files(i).name); % Sheets array.
% Get whole datas inside the excel files month by month.
for j=1:length(sheets)
data = readtable(files(i).name, "Sheet", sheets(j));
days = 1:1:size(data.Date); % Days array.
hold on;
plot(data.number_sold, days, "b.", "LineWidth", 2) % xAxis = sold_number
% yAxis = days
end
end
hold off;
end
% Call function "plotter"
xlsx_files = dir("*.xlsx");
Maindir = "C:\Users\emird\AppData\Local\Programs\df_2_db";
plotter(xlsx_files, Maindir)
When i call my funciton i get an error.
How can solve this problem and run my code?

7 Comments

Where did you save the PLOTTER function? What filename did you use for it?
Saved same directory with my matlab code. and ı used same name with my function plotter
MATLAB cannot find your file. So you need to check that the function is saved on the MATLAB search path (or in the current directory) in a file named "plotter.m". Make sure that you do not have any other code in the file, apart from the function definition.
This is the screen shot of where my matlab code and "plotter" function be in. When i run my code it changes current directory to where the excel files stored in(gets in the ExcelFiles directory).
Oh okey i solvedthe problem. My script function "plotter" has to be in the ExcelFiles directory. When i have putted on this folder everything works. On the other hand i don't understand why it works?
"When i run my code it changes current directory to where the excel files stored in(gets in the ExcelFiles directory)."
Which is one of the reasons why it is a bad idea to change directory just to access data files: your code changes the function scope.... and then functions don't work, because you have changed the function scope. Best avoided.
The recommended approach is to use absolute/relative filenames to access data files. All MATLAB functions that access data files accept absolute/relative filenames. You should use absolute/relative filenames. You will find FULLFILE useful for this.
Okey thank you very much.

Sign in to comment.

Answers (1)

Based on the code you have shared, you likely need to change the order of your script. Functions must be defined at the bottom of the script.
% Call function "plotter"
xlsx_files = dir("*.xlsx");
Maindir = "C:\Users\emird\AppData\Local\Programs\df_2_db";
plotter(xlsx_files, Maindir)
function plotter(files, dir_path)
Exceldir = dir_path + "\ExcelFiles";
cd(Exceldir); % Change current directory to the where excel files restored in.
for i=1:length(files)
% Get sheet names being in the excel file.
sheets = sheetnames(files(i).name); % Sheets array.
% Get whole datas inside the excel files month by month.
for j=1:length(sheets)
data = readtable(files(i).name, "Sheet", sheets(j));
days = 1:1:size(data.Date); % Days array.
hold on;
plot(data.number_sold, days, "b.", "LineWidth", 2) % xAxis = sold_number
% yAxis = days
end
end
hold off;
end

8 Comments

Oh i get it and thanks for this but i created a funciton script for this function and saved that function script same folder in with my matlab code. How can i call function from function script i still get same error as i mentiod on my question?
Good question. I'm not sure, as I can't duplicate the error.
Run the following and share the result
which plotter
Try clearing your workspace and then running your code again. Let us know what happens.
inside of the "DF_2_DB" folder
I wrote python code for pull data from different files and upload these data to databases and excel files.
I am trying to get excel files all datas and plot them. My matlab code works fine but ı want to plot these datas
using a function script "plotter". I started matlab few months ago so i don't understand what you are staing me to do. I share every file for you to work on this.
Oh okey i solvedthe problem. My script function "plotter" has to be in the ExcelFiles directory. When i have putted on this folder everything works. On the other hand i don't understand why it works?
Take a look at this page about the Function Precedence Order in MATLAB.
When you use cd, you change your current folder. That is making the function file unaccessable to MATLAB. The solution could be any of these options
  1. Move the function file to the folder that is 'current' at that point in your script
  2. Add the folder containing your script to your MATLAB path (see here)
  3. Create the function as an in-file function (see here)
"The solution could be any of these options"
The much better solution is to use absolute/relative filenames to access data files.
okey i get it. Thank you for everything.
+1 to Stephen23's comment.

Sign in to comment.

Products

Asked:

on 27 Feb 2023

Commented:

on 27 Feb 2023

Community Treasure Hunt

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

Start Hunting!