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
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
2012 年 7 月 25 日
回答 (1 件)
Image Analyst
2012 年 7 月 25 日
編集済み: Image Analyst
2012 年 7 月 25 日
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
2012 年 7 月 25 日
編集済み: Walter Roberson
2012 年 7 月 25 日
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
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
2012 年 7 月 25 日
Jim
2012 年 7 月 25 日
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
2012 年 7 月 25 日
この質問は閉じられています。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!