필터 지우기
필터 지우기

how to read multiple files using a function from the previous script

조회 수: 3 (최근 30일)
johnson saldanha
johnson saldanha 2018년 12월 19일
편집: Jan 2018년 12월 19일
in my 1st script i created the whole code as a function and it returns me a single value in the end. the first two lines of my code are
function rated= range(file_name) %rated is the final output which returns the single output and range is the name of the script.
T=readtable(file_name);
in my second script i need to read multiple files and gets outputs for each script. the code in the second script is
Tables=['200 feb.xlsx', '390 feb.xlsx'];
rated= [];
for i=1:size(Tables, 2)
disp(Tables(i));
rated=[rated(:); range(Tables(i))];
end
and im getting an error which is as follows. range is my first script and rangenumbers is my second script. could u help me
Error using table/readTextFile (line 80)
Unable to open file 'c.txt'.
Error in table.readFromFile (line 41)
t = table.readTextFile(filename,otherArgs);
Error in readtable (line 114)
t = table.readFromFile(filename,varargin);
Error in range (line 2)
T=readtable(file_name);
Error in rangennumbers (line 6)
rated=[rated(:); range(Tables(i))];
  댓글 수: 4
johnson saldanha
johnson saldanha 2018년 12월 19일
do u think theres an error in this
function rated= range(file_name) %rated is the final output which returns the single output and range is the name of the script.
T=readtable(file_name);
Walter Roberson
Walter Roberson 2018년 12월 19일
Your Tables is a character vector . You pass a single character of that vector to range()
You need to switch to cell array like Rik said.

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

채택된 답변

Jan
Jan 2018년 12월 19일
편집: Jan 2018년 12월 19일
In Matlab there is an essential difference between "scripts" and "functions". It is important not to confuse the terms. "Functions" start with the word "function" and have inputs and outputs, in opposite to scripts, which share the workspace with their callers.
A problem of your code is:
Tables=['200 feb.xlsx', '390 feb.xlsx'];
This concatenates the two char vectors, such that you get:
Tables = '200 feb.xlsx390 feb.xlsx';
Rik suggested to use a cell string instead with curly braces instead of the square brackets:
Tables = {'200 feb.xlsx', '390 feb.xlsx'};
But this does not match to the error message: I have no idea where "c.txt" is coming from, so I assume you do not show us the failing code but another one.
The next problem is, that your function range() does not define its output rated. Perhaps you want to return the imported table T?
function T = range(file_name)
T = readtable(file_name);
end
Finally:
Tables = {'200 feb.xlsx', '390 feb.xlsx'};
rated = [];
for i = 1:size(Tables, 2)
disp(Tables{i}); % Curly braces
rated = [rated; range(Tables{i})];
end
The iterative growing of arrays is very inefficient, because the runtime grows exponentially. Prefer a "pre-allocation".

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by