MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by huda nawaf on 25 Jul 2012

hi,

how I can read such txtfile

1::1193::5::978300760 1::661::3::978302109 1::914::3::978301968 1::3408::4::978300275 1::2355::5::978824291 1::1197::3::978302268 1::1287::5::978302039

when I used textscan, I'm not get the data thanks

*No products are associated with this question.*

Answer by per isakson on 25 Jul 2012

Edited by Ned Gulley on 26 Jul 2012

Accepted answer

How I can read a text file, which looks like this in an editor

1::1193::5::978300760 1::661::3::978302109 1::914::3::978301968 1::3408::4::978300275 1::2355::5::978824291 1::1197::3::978302268 1::1287::5::978302039

I want a [7x7] double array

I have tried with the following command

cac = textscan( fid, '%d%d%d%d%d%d%d', 'Delimiter', ':' );

when I used this command, I'm not get the data thanks

I guess this is a more effective way to present the question - per

.

**--- Problem solved ---**

Thanks Jan, your hint guided me to a solution.

I changed my mind. Now, I want a [7x4 int32] and hope that ":" always comes in pairs.

>> txt2m() ans = [7x4 int32] >> cac{:} ans = 1 1193 5 978300760 1 661 3 978302109 1 914 3 978301968 1 3408 4 978300275 1 2355 5 978824291 1 1197 3 978302268 1 1287 5 978302039

where the function, txt2m, is given by

function cac = txt2m() %% fid = fopen('cssm.txt'); cac = textscan( fid, '%d%d%d%d' ... , 'Delimiter' , ':' ... , 'CollectOutput' , true ... ... , 'EmptyValue' , -999 ... , 'ExpChars' , '' ... , 'MultipleDelimsAsOne' , true ... , 'Whitespace' , '' ); fclose( fid ) end

## 10 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/44458#comment_91449

http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/44458#comment_91450

Please show your textscan attempt.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/44458#comment_91453

What is the proper form of the data? From the one you've shown, I assume that each line should be a separate row of four integers-- is this correct? Do the colons have a particular meaning aside from separating columns? Are you ignoring any of the columns?

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/44458#comment_91482

this is the code

f = fopen('d:\matlab\r2011a\bin\movielens\ratings_1m.dat'); c = textscan(f, '%d %d %d %d'); c1=c{1}

and this is the result:

c =

c1 =

thanks

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/44458#comment_91484

hi Matt yes my data as

1::1193::5::978300760

1::914::3::978301968

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/44458#comment_91485

What do you want as output? You forgot to explain this very important detail.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/44458#comment_91541

jan what I need is each column alon for example: c1=c{1},c2=c{2} and so on

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/44458#comment_91562

And what how many columns do you want? Is "::" the separator and you want 4 columns of data, or is ":" the separator and you get 7 columns, while the 2nd, 4th and 6th get invalid values - if so, which invalid values?

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/44458#comment_91614

hi huda, Why do you include space in your format-string, "'%d %d %d %d'"? There are no spaces in the text-file.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/44458#comment_91618

119 questions asked so far but the style hasn't changed by a comma from the initial requests. I'm still wandering why bother at all...