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!