how to initialize the data from .txt data?

4 ビュー (過去 30 日間)
EDWARD IJAU PELIAS POG
EDWARD IJAU PELIAS POG 2019 年 4 月 7 日
hi.. i tried to run my code to initilize my data from .txt file name DATANAME.txt and TESTRATE.txt. (see the attachment). the code were as folow; the DATANAME.txt contain 6X2500 matrix while the TESTRATE.txt contain 6X25001 matrix. but when i tried to run the code, it give me this message (load °Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç](a lot of them)
Error using load
Must be a string scalar or character vector.
Error in initialize (line 12)
dataset=load(DATANAME); )
can anyone tell me the problem ? is it because of my .txt file? thank you..
function [train,train_n,test,test_n,N,D,C]=initialize(DATANAME,TESTRATE)
%train: Training Data
%train_n: Number of Training Data
%test: Test Data
%test_n: Number of Test Data
%N: Total Number of Data
%D: Input Dimension + 1 (class label)
%C: Maximum Number of Classes (may not be equal to number of classes)
% load dataset
fprintf(2,'load %s\n',DATANAME);
dataset=load(DATANAME);
%if 'train' and 'test' in another file, combine.
tops=findstr(DATANAME,'train');
if 0 < length(tops)
train=dataset;
TESTNAME=strcat(DATANAME(1:tops-1),'test',DATANAME(tops+5:length(DATANAME)));
test=load(TESTNAME);
fprintf(2,'load %s\n',TESTNAME);
dataset=[train;test];
end
[N,D]=size(dataset);
% if class '0' exist, then class+1
if 0<size(find(dataset(:,D)<1),1)
dataset(:,D)=dataset(:,D)+1;
end
C= max(dataset(:,D));
fprintf('%d-samples %d-dim %d-class ',N,D-1,C);
c=zeros(1,C);
fprintf('[ ')
for i=1:C
c(i)=sum( dataset(:,D) == i );
fprintf('%d ',c(i))
end
fprintf(']\n')
%shuffle dataset
dataset=shuffle(dataset);
%normalize dataset
mindata=min(dataset(:,1:D-1));
width=max(dataset(:,1:D-1))-min(dataset(:,1:D-1));
dataset(:,1:D-1)=( dataset(:,1:D-1)-repmat(mindata,N,1) )./repmat(width,N,1);
%devide dataset into 'train' and 'test'
test_n=ceil(N*TESTRATE);
train_n=N-test_n;
train=dataset(1:train_n,:);
if train_n==N
test=train;
else
test=dataset(train_n+1:N,:);
end
clear dataset

採用された回答

Walter Roberson
Walter Roberson 2019 年 4 月 10 日
Your code expects you to pass file names as the first two parameters. You are passing the content of the files instead.
  2 件のコメント
Walter Roberson
Walter Roberson 2019 年 4 月 12 日
You are using dlmread() to read the contents of the files into m1 and m2, and then you are passing those contents to initialize() . But your code for initialize() takes those parameters and attempts to load() the parameter. That fails because you are passing in the data already, not the name of the data file. If you are wanting to pass in the data itself, then you should not be trying to load the data.
I suggest that instead of doing the dlmread() that you simply pass the file names to initialize().
It should be obvious from the code in initialize() that file names are expected: look at the variable names you are using, like DATANAME -- name of the data file, not content of the data file!
EDWARD IJAU PELIAS POG
EDWARD IJAU PELIAS POG 2019 年 4 月 14 日
i got it already...thanks for the help!

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

その他の回答 (1 件)

per isakson
per isakson 2019 年 4 月 7 日
編集済み: per isakson 2019 年 4 月 7 日
Try
>> m1 = dlmread('H:\m\cssm\TESTRATE.txt');
>> m2 = dlmread('H:\m\cssm\DATANAME.txt');
>> whos m*
Name Size Bytes Class Attributes
m1 5x2501 100040 double
m2 5x2500 100000 double
  9 件のコメント
per isakson
per isakson 2019 年 4 月 11 日
編集済み: per isakson 2019 年 4 月 11 日
Your question suggested that load() of your files throwed errors with a message containing garbage. I fail to understand what caused the error you encountered.
I downloaded your text files and read them without problem with dlmread(). Consequently, I hinted that you should replace load() by dlmread(). Thus, I intended that you should have modified initialize() by replacing load() by dlmread().
The value of the variable DATANAME is that 'DATANAME.txt' ?
per isakson
per isakson 2019 年 4 月 11 日
Now I understand a bit better
fprintf(2,'load %s\n', DATANAME );
throws the error
load °Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç]°Æç] etc.
if DATANAME is an array of double. But that is not an error using load

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by