how to initialize the data from .txt data?

4 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 10 Apr 2019
Your code expects you to pass file names as the first two parameters. You are passing the content of the files instead.
  2 Comments
Walter Roberson
Walter Roberson on 12 Apr 2019
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 on 14 Apr 2019
i got it already...thanks for the help!

Sign in to comment.

More Answers (1)

per isakson
per isakson on 7 Apr 2019
Edited: per isakson on 7 Apr 2019
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 Comments
per isakson
per isakson on 11 Apr 2019
Edited: per isakson on 11 Apr 2019
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 on 11 Apr 2019
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

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!