how to initialize the data from .txt data?

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

Your code expects you to pass file names as the first two parameters. You are passing the content of the files instead.

2 Comments

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!
i got it already...thanks for the help!

Sign in to comment.

More Answers (1)

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

hi...thanks for the respond...i tried but still showing the same error
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:
  1. 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.)
  2. Describe exactly how you call dmlread() to get this weird error.
Maybe it something to do with "Must be a string scalar or character vector.". do u know how to convert the .txt file to string scalar or character vector?
You didn’t respond to Per’s second proposal.
>>m1=dlmread('H:\m\cssm\TESTRATE.txt');
>>m2=dlmread('H:\m\cssm\DATANAME.txt');
>>initialize (m2,m1)
hi madhan ravi..here it is...thanks.
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
  1. did you download copies of the text files from your question? I guess not.
  2. do you have a folder named H:\m\cssm\ on your system? I guess not.
  3. did dlmread() throw this error? I guess not.
  4. 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
  1. show exactly and in detail the commands you invoke. Copy&Paste from the command window.
  2. describe the variables involved. Use whos name1 name2 etc and Copy&Paste the result from the command window.
  3. show the exact output, e.g. error message, in the command window. Use Copy&Paste from the command window.
hi per...
  1. yes...i had download it.
>> m1=dlmread('C:\Users\Edward\Documents\Custom Office Templates\MATLAB\Pics\A\1000\face\DATANAME1.txt');
>> m2=dlmread('C:\Users\Edward\DOcuments\Custom Office Templates\MATLAB\Pics\A\1000\face\TESTRATE2.txt');
3. no. The command works just fine.
4. the coding is still the same as i posted here.. but when i execute the function;
initialize(m1,m2)
it give me this error;
Error using load
Must be a string scalar or character vector.
Error in initialize (line 12)
dataset=load(DATANAME);
----------------------------------------------------------------
if you refer to my code function that i had posted. the error is from here;
% load dataset
fprintf(2,'load %s\n',DATANAME);
dataset=load(DATANAME);
------------------------------------------------------------------------------------------------------------
my .txt file has 5 rows and 2500 columns. each row represent the image data per person. so each row represent 1 person.
-----------------------------------------------------------------------------------------------------------
i tried to use the 'ImportData' tools and change the 'Output Type' to 'string array'..and called it using initialize().. it works but still showing the same error at the ;
% load dataset
fprintf(2,'load %s\n',DATANAME);
dataset=load(DATANAME);
which is
-
-
-
load 190
load 226
load 45
load 29
load 185
load 190
load 226
load 45
load 29
Error using load
Must be a string scalar or character vector.
Error in initialize (line 12)
dataset=load(DATANAME);
thanks sir. i am new to matlab software. ;-)
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' ?
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.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!