loading text file to matrix without delimiters

I have a text file dataset with Y number of lines and each line has exactly 250 characters (including spaces).
I want to put each character (even if it is a space) into a matrix so I create a matrix with 250 columns and Y rows. We can replace the spaces with NaN.
I have been playing with textscan and few other functions but cannot seem to get it. Anyone have any ideas?

 Akzeptierte Antwort

Jos (10584)
Jos (10584) am 2 Apr. 2014

0 Stimmen

So each digit is a single value, and spaces are to be replaced with NaN …
M = char(textread('example.txt','%s','delimiter','')) - '0' ;
M(M==(' '-'0')) = NaN ; % replaces spaces with NaN's
% M is now a 4-b-250 numerical array
btw the second line of example.txt is 251 characters long

Weitere Antworten (5)

Azzi Abdelmalek
Azzi Abdelmalek am 2 Apr. 2014

0 Stimmen

d=importdata('file.txt')

3 Kommentare

Ammar
Ammar am 2 Apr. 2014
Bearbeitet: Ammar am 2 Apr. 2014
Importdata creates the correct number of rows but it uses spaces as a delimiter to create only a few columns.
So for: 4334___34343_455656435356345__4534
where _ = space
it would create 4 columns. Meanwhile, I want each character or space to be put into a separate column
This is not clear. Do you want to import numeric data or what? Post the four line of your file
Ammar
Ammar am 2 Apr. 2014
I have attached an example file. There are 4 lines Each line has 250 characters (including spaces)
I want to turn this into a matrix with 250 columns and 4 rows

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 2 Apr. 2014

0 Stimmen

When I saved your example it didn't have exactly 250 characters on every line. I had 251 in the second line for some reason and the last line had only 1 character. So I made the code a little more robust than you might need it, but extra robustness never hurts.
fid = fopen('example.txt');
tline = fgetl(fid);
lineCounter = 1;
charArray = tline;
fprintf('%d characters in line #%d: %s\n', length(tline), lineCounter, tline)
while ischar(tline)
tline = fgetl(fid);
if length(tline) < 3
continue;
end
fprintf('%d characters in line #%d: %s\n', length(tline), lineCounter, tline)
charArray = [charArray; tline(1:250)];
lineCounter = lineCounter + 1;
end
fclose(fid);
% Display in command window.
charArray
Of course you can get rid of the display lines (fprintf, etc.) if you want.
Ammar
Ammar am 2 Apr. 2014

0 Stimmen

Thank you everybody!! I had been banging my head on the wall for a few hours on this item.
Joseph Cheng
Joseph Cheng am 2 Apr. 2014
Bearbeitet: Joseph Cheng am 2 Apr. 2014

0 Stimmen

simple method
fid = fopen('example.txt','r');
line = fgetl(fid);
spaces = strfind(line,' ');
line(spaces)=0;
x=line(:);
y=hex2dec(x)';
y(spaces)=NaN;
this is just an example but given the line of text use strfind(line,' ') to find the spaces. Note these indexes to be replaced by NaNs, make the spaces 0 and transpose it to a 250x1 array such that hex2dec will convert each into a number, then replace the index of the found spaces to NaN.
I used hex2dec as i noticed in your example you have a 'C' in there. So unless it was a typo its a hex number? if it is a typo then just use str2num or str2double instead
Ammar
Ammar am 3 Apr. 2014
Bearbeitet: Ammar am 3 Apr. 2014

0 Stimmen

Instead of putting one character per cell is there a way to specifcy which cell they go into?
For example, if I have text like the following
24359_435934009____________90909
where _=space
And I want to put it into a matrix such that
characters 1-5 go into column 1 (a.k.a 24359)
character 6 goes into column 2 (ak.a. NaN)
characters 7-10 goes into column 3
and so on....

4 Kommentare

Is this an answer or a question?
Ammar
Ammar am 3 Apr. 2014
new question (Variation to the question)
use my method i had and modify it.
fid = fopen('example.txt','r');
line = fgetl(fid);
spaces = strfind(line,' ');
spaces = [0 spaces length(line)+1];
cellpos = 1;
for i=1:length(spaces)-1
portion = line(spaces(i):spaces(i+1)-1);
if portion ==' '
blah{cellpos}=NaN;
else
blah{cellps}=portion;
end
Ammar, this is considered as an answer, you can add comments by clicking on [comment on this answer]* or create a new question

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by