필터 지우기
필터 지우기

How can i call function without any output arguments?

조회 수: 18 (최근 30일)
Emir Dönmez
Emir Dönmez 2023년 2월 27일
댓글: Cris LaPierre 2023년 2월 27일
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
Stephen23
Stephen23 2023년 2월 27일
편집: Stephen23 2023년 2월 27일
"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.
Emir Dönmez
Emir Dönmez 2023년 2월 27일
Okey thank you very much.

댓글을 달려면 로그인하십시오.

답변 (1개)

Cris LaPierre
Cris LaPierre 2023년 2월 27일
편집: Cris LaPierre 2023년 2월 27일
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
Emir Dönmez
Emir Dönmez 2023년 2월 27일
okey i get it. Thank you for everything.
Cris LaPierre
Cris LaPierre 2023년 2월 27일
+1 to Stephen23's comment.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by