importing text and integers using textscan and using in a matrix

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 件のコメント

Image Analyst
Image Analyst 2012 年 7 月 25 日
編集済み: Image Analyst 2012 年 7 月 25 日
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
Jim 2012 年 7 月 25 日
thanks

回答 (1 件)

Image Analyst
Image Analyst 2012 年 7 月 25 日
編集済み: Image Analyst 2012 年 7 月 25 日

0 投票

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 件のコメント

Jim
Jim 2012 年 7 月 25 日
編集済み: Walter Roberson 2012 年 7 月 25 日
thanks, but my problem is getting the data imported so I can randomize the rows and do other things. I know how to randomize - when I try to import the data, there seems to be a problem with one row text and one integer. specifically:
words2id = fopen('words2.txt');
words2 = textscan(words2id, '%s %d')
do stuff
then
text = words2(2,1);% word/nonword on this trial
this produces the following error
Index exceeds matrix dimensions.
Walter Roberson
Walter Roberson 2012 年 7 月 25 日
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
Image Analyst 2012 年 7 月 25 日
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
Jim 2012 年 7 月 25 日
Walter,
Thanks. This displays the word in the first row, but when I change it to:
words2{2}{1}
I get the following error:
Cell contents reference from a non-cell array object.
There are over a thousand words in the col and I'll need to display all of them at some point.
Jim
Jim 2012 年 7 月 25 日
OK, I can display and text in the list with this code:
text = words2{1}{n}%n is any row
I want to then sort the rows according to the integer in the second col. how do I do that?
Image Analyst
Image Analyst 2012 年 7 月 25 日
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
Jim 2012 年 7 月 25 日
That does not work. The error message is:
Undefined function or method 'str2doule' for input arguments of type 'int32'.
words2 contains {10x1 cell} [10x1 int32]
I'm still relatively new to Matlab.
thanks for your help.

この質問は閉じられています。

質問済み:

Jim
2012 年 7 月 25 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by