how to initialize the data from .txt data?
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (1)
per isakson
el 7 de Abr. de 2019
Editada: per isakson
el 7 de Abr. de 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 comentarios
EDWARD IJAU PELIAS POG
el 7 de Abr. de 2019
per isakson
el 7 de Abr. de 2019
Editada: per isakson
el 7 de Abr. de 2019
Weird! It's impossible for me to understand what you are doing. It works fine for me on R2018b, Windows10. dlmread() has been around for a long time.
Your files are simple tab-delimited text files. The copies I downloaded are UTF-8 encoded. Could it be that the encoding has been changed by the up and download?
Proposal:
- Download copies from your question and try to read these copies with dlmread(). (Don't forget the extension, ".txt". It belongs to the name of the file.)
- Describe exactly how you call dmlread() to get this weird error.
EDWARD IJAU PELIAS POG
el 7 de Abr. de 2019
madhan ravi
el 7 de Abr. de 2019
You didn’t respond to Per’s second proposal.
EDWARD IJAU PELIAS POG
el 7 de Abr. de 2019
Editada: per isakson
el 8 de Abr. de 2019
per isakson
el 8 de Abr. de 2019
Editada: per isakson
el 8 de Abr. de 2019
Hi EDWARD, Your comments does little to help us find a solution to your problem. For example
>>m1=dlmread('H:\m\cssm\TESTRATE.txt');
>>m2=dlmread('H:\m\cssm\DATANAME.txt');
>>initialize (m2,m1)
is not an exact desciption of how you called dlmread(). The two first lines are literal copies from my answer. (When answering questions, I assume that the questioner has some basic knowledge of Matlab and uses Matlab's help system to fill in.) Your recent comment raises questions
- did you download copies of the text files from your question? I guess not.
- do you have a folder named H:\m\cssm\ on your system? I guess not.
- did dlmread() throw this error? I guess not.
- initialize() is that the function of your question or a modified version? The former requires that the values of input arguments are strings (or character rows). m1 and m2 are arrays of double (if dlmread() succeded).
To approach a solution to your problem you need to
- show exactly and in detail the commands you invoke. Copy&Paste from the command window.
- describe the variables involved. Use whos name1 name2 etc and Copy&Paste the result from the command window.
- show the exact output, e.g. error message, in the command window. Use Copy&Paste from the command window.
EDWARD IJAU PELIAS POG
el 10 de Abr. de 2019
per isakson
el 11 de Abr. de 2019
Editada: per isakson
el 11 de Abr. de 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
el 11 de Abr. de 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
Categorías
Más información sobre Environment and Settings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!