Reputation: 53
I have data that looks like this:
# input 1
0 1 1 0 0 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 1 0 1 1 0 1 0 0 1 1 0 0 1 0 0 0 1 0 1 0 0 1 0 1 0 1 1 0 0 1 1 0 0 1 0 0 0 0 1 1 0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 0 1 1 0 1 0 0 1 1 0 0 1 1 0 1 0 0 0 0 0 0 1 0 0 1 0 1 0 1 0 1 0 1 0 0 1
# output 1
1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
# input 2
0 1 0 1 0 0 0 1 0 1 0 1 0 1 1 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 1 0 1 1 0 1 0 0 1 1 0 0 1 0 0 0 1 0 1 0 0 1 0 1 0 1 1 0 0 1 1 0 0 1 0 0 0 0 1 1 0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 0 1 1 0 1 0 0 1 1 0 0 1 1 0 1 0 0 0 0 0 0 1 0 0 1 0 1 0 1 0 1 0 1 0 0 1
# output 2
1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
The data is in a text file. There are about 130 input and output vectors. I basically need to load the data in MATLAB for training a SVM model. Does anybody know how I can load the data into MATLAB?
Thanks!
Upvotes: 0
Views: 393
Reputation: 32930
A shorter way of loading your text file:
%// Read lines from text file into cell array
fid = fopen(filename, 'r');
C = textscan(fid, '%s', 'Delimiter', '', 'CommentStyle', '#');
fclose(fid);
%// Convert contents of each cell into a numerical array
C = cellfun(@str2num, reshape(C{1}, [], 2), 'UniformOutput', false);
The result is a N×2 cell array C
(where N is the number of input-output pairs) containing your data.
For the example in your question, C{1, 1}
stores an input 1×121 array of 1's and 0's, and C{1, 2}
contains the corresponding 1×10 output array [1.00 0 0 0 0 0 0 0 0 0]
.
Upvotes: 1
Reputation: 46435
If you know the lines you don't want start with #
, you can use something like textscan
with a simple option: "stop reading when you reach this character". In that case, reading one line at a time you would get
fid = fopen(myFile);
while(~feof(fid))
myData = textscan(fid, '%[^#]d');
if numel(myData) > 1
thisRow = [myData{:}];
end
end
I don't have access to matlab right now but the above should be very close...
I just realized that if your data is in fact alternating "name of vector" and "data of vector", and the name is always preceded by a "#", you can do better:
fid = fopen(myFile);
while(~feof(fid))
myName = textscan(fid, '# %s %d');
myData = textscan(fid, '%f', Inf);
if numel(myData) > 1
varName = sprintf('%s{%d}', myName{1}, myName{2});
assignin('base', varName,myData);
end
end
This might just do what you really need... or, again, be somewhat close. Experiment with these commands - treat it "for inspiration only".
Upvotes: 0
Reputation: 1310
You can import text files into MATLAB both interactively as well as programmatically.
To import data interactively, use the Import Tool. You can generate code to repeat the operation on multiple similar files. The Import Tool supports text files, including those with the extensions .txt, .dat, .csv, .asc, .tab, and .dlm. These text files can be nonrectangular, and can have row and column headers. Data in these files can be a combination of numeric and nonnumeric text, and can be delimited by one or more characters.
To import data from text files programmatically, use an import function. Most of the import functions for text files require that all data fields in your file are numeric, and that each row of data has the same number of column
There are many import tools in MATLAB like 'load','textscan',etc
Upvotes: 0