How to load two ARFF file in Matlab?

6 ビュー (過去 30 日間)
afef
afef 2017 年 4 月 24 日
編集済み: Prasanna 2025 年 6 月 4 日
I want to load ARFF file to matlab and i find this function
function wekaOBJ = loadARFF(filename)
import weka.core.converters.ArffLoader;
import java.io.File;
loader = ArffLoader();
loader.setFile(File('train.arff'));
wekaOBJ = loader.getDataSet();
wekaOBJ.setClassIndex(wekaOBJ.numAttributes -1);
end
But when i try to load 2 ARFF files with this code i only get the last file.How can i solve this ?
function wekaOBJ = loadARFF(filename)
import weka.core.converters.ArffLoader;
import java.io.File;
loader = ArffLoader();
loader.setFile(File('train.arff'));
wekaOBJ = loader.getDataSet();
wekaOBJ.setClassIndex(wekaOBJ.numAttributes -1);
loader = ArffLoader();
loader.setFile(File('test.arff'));
wekaOBJ = loader.getDataSet();
wekaOBJ.setClassIndex(wekaOBJ.numAttributes -1);
end

回答 (1 件)

Prasanna
Prasanna 2025 年 6 月 4 日
編集済み: Prasanna 2025 年 6 月 4 日
Hi Afef,
It is my understanding that you're trying to load two ARFF files ("train.arff" and "test.arff") into MATLAB using the "loadARFF" function, which relies on Weka's Java API. However, you're only getting the data from the second file ("test.arff")
The issue arises because your current function only returns one output ("wekaOBJ"), and you're loading both files sequentially into the same variable. The second load overwrites the first. To solve this, you can store each dataset in separate variables. The below function improves on your function and loads both files:
function [trainData, testData] = loadARFF()
import weka.core.converters.ArffLoader;
import java.io.File;
% Load train.arff
loader = ArffLoader();
loader.setFile(File('train.arff'));
trainData = loader.getDataSet();
trainData.setClassIndex(trainData.numAttributes - 1);
% Load test.arff
loader = ArffLoader(); % Create a new loader instance
loader.setFile(File('test.arff'));
testData = loader.getDataSet();
testData.setClassIndex(testData.numAttributes - 1);
end
Your previous approach replaced the first dataset with the second. The above function ensures both "train.arff" and "test.arff" are loaded independently and can be processed further.
Hope this helps!

カテゴリ

Help Center および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by