Error Attempt to execute SCRIPT join_function as a function:

I'm trying to merge 100 tables from a cell array. I'm trying to use loop that calls the function 'join_function' but I get this error. Can anyone help?
join_function:
c = outerjoin(data{1,1},data{1,2},'Keys','time','MergeKeys',true);
loop:
numfiles = 100;
demand = table(1, numfiles);
for j = 1:numfiles
demand_file = sprintf('data{1,%d}', j);
demand{k} = join_function(demand_file);
end
error message from commanf window:
Attempt to execute SCRIPT join_function as a function:
C:\Users\Mark\Downloads\join_function.m
Error in testjoinall (line 14)
demand{k} = join_function(demand_file);

 採用された回答

Jan
Jan 2018 年 1 月 30 日
編集済み: Jan 2018 年 1 月 30 日

0 投票

The error message is clear: join_function.m is a script file and then it does not accept input arguments. Only functions do this. See https://www.mathworks.com/help/matlab/matlab_prog/scripts-and-functions.html and https://www.mathworks.com/help/matlab/learn_matlab/scripts-and-functions.html .
The code has more problems. sprintf('data{1,%d}', j) creates strings, char vectors to be exact. But in the joining you try to access the contents of an data array. This is something completely different. 'data{1,2}' is a char vector, but does not carry any information about the array called "data".
It is strange, that you create 'data{1,1}' in the loop, but try to access the two elements of the cell array data{1,1} and data{1,2} inside the outerjoin function.
I guess, you want something like this:
numfiles = 100;
demand = table();
for j = 1:numfiles
demand = outerjoin(demand, data{1, j}, 'Keys', 'time', 'MergeKeys', true);
end

5 件のコメント

Mark Rodger
Mark Rodger 2018 年 1 月 30 日
Thanks, I've tried this but I get an error saying "unrecognized varaible name 'time'." Do you know how to over come this?
Walter Roberson
Walter Roberson 2018 年 1 月 30 日
編集済み: Walter Roberson 2018 年 1 月 30 日
outerjoin is a function that applies to table objects and timetable objects. When you specify 'Keys', 'time', that tells MATLAB that it should look inside the table for a column named 'time' and use that column to do the join. The message you are getting is because your table does not contain 'time'
Mark Rodger
Mark Rodger 2018 年 1 月 31 日
But all the tables have 'time'.
Walter Roberson
Walter Roberson 2018 年 1 月 31 日
Jan's suggested code
demand = table()
created a table with no variables. You would need to omit that line
Jan
Jan 2018 年 1 月 31 日
編集済み: Jan 2018 年 1 月 31 日
You can omit the initialization with an empty table, but use the first table and start the concatenation at the 2nd one:
numfiles = 100;
demand = data{1, 1};
for j = 2:numfiles
demand = outerjoin(demand, data{1, j}, 'Keys', 'time', 'MergeKeys', true);
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

タグ

質問済み:

2018 年 1 月 30 日

編集済み:

Jan
2018 年 1 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by