importing text and integers using textscan and using in a matrix
Info
This question is closed. Reopen it to edit or answer.
Show older comments
I have a text file with 2 cols. Col one is text and col is integer. I want to import the two cols and then be able to manipulate the imported set, eg, randomize the order of the rows. I've tried textscan and xlsread but I cannot work with the imported data in matlab. for example, I have word1 3 word2 5 word3 5 . . . and want to randomize the order of the rows.
3 Comments
Image Analyst
on 25 Jul 2012
Edited: Image Analyst
on 25 Jul 2012
You say "I have a text file with 2 cols" and then "I have word1 3 word2 5 word3 5 . . . " Please tell us exactly how can that be considered 2 columns. It looks like at least 6 columns to me, maybe more if that's what dot dot dot meant.
Oh wait, never mind. When I went to edit your post, I can see that you forgot to format your rows as code so that it looks like what you want it to look like. I'm not going to do it for you because I want you to learn how. Highlight the text, then click the "{} Code" icon right above the text box.
Jim
on 25 Jul 2012
Answers (1)
Image Analyst
on 25 Jul 2012
Edited: Image Analyst
on 25 Jul 2012
Why don't you use dlmread()?
Demo to randomize rows:
% Generate sample data.
m = magic(6)
% Get a random order for the rows.
randomRows = randperm(size(m, 1))
% Extract rows in that random order into a new matrix.
randomized_m = m(randomRows, :)
7 Comments
Jim
on 25 Jul 2012
Edited: Walter Roberson
on 25 Jul 2012
Walter Roberson
on 25 Jul 2012
words2{1}{1} perhaps ?
words2{1} would be a cell array of strings, corresponding to all of the column 1. words2{2} would be a numeric column vector corresponding to all of the column 2. words2 itself is a cell row vector, words2(1,1) words2(1,2) but words2(2,1) does not exist.
Image Analyst
on 25 Jul 2012
If some of the rows may have problems, like they're not formatted like they're supposed to be, then you might have to handle that situation, such as a try/catch where in the catch you have a "continue" line to skip processing that line and move on to the next line.
Jim
on 25 Jul 2012
Jim
on 25 Jul 2012
Image Analyst
on 25 Jul 2012
Can't you just use str2double to convert the string version of the numbers into double, and then use sort()? That's seems like the obvious procedure, so I'm sure you already tried that. So what happened when you tried it?
Jim
on 25 Jul 2012
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!