reading from txtfile

조회 수: 1 (최근 30일)
Sukuchha
Sukuchha 2012년 4월 30일
i have a txt file,from which i want to read following lines into matlab. How can I read it ?
class lookup = {
0, 0, 0, 0, 255, 0, 0, 0, 255, 255, 127, 80, 46, 139, 87,
0, 255, 255}
class names = {
Unclassified, forest [Green] 500 points, Water [Blue] 500 points,
baresoil [Coral] 500 points, Vegetation [Sea Green] 500 points,
Random Sample (Random Sample (builtup)) [Cyan] 500 points}
I want class lookup in nx3 matrix and out of class names i need only class names ( forest, water, baresoil, vegetation, builtup)
  댓글 수: 3
Sukuchha
Sukuchha 2012년 4월 30일
Lookup is a color triplets (RGB) information representing color of 6 class in this case.
I tagged fgetl becuase i thought the best way is to read the file line by line .
Sukuchha
Sukuchha 2012년 4월 30일
Lookup is a color triplets (RGB) information representing color of 6 class in this case.
I tagged fgetl becuase i thought the best way is to read the file line by line .

댓글을 달려면 로그인하십시오.

채택된 답변

per isakson
per isakson 2012년 4월 30일
Try this
lookup = { 0, 0, 0, 0, 255, 0, 0, 0, 255, 255, 127, 80, 46, 139, 87, 0, 255, 255};
names = { 'Unclassified' ...
, 'forest [Green] 500 points' ...
, 'Water [Blue] 500 points' ...
, 'baresoil [Coral] 500 points' ...
, 'Vegetation [Sea Green] 500 points' ...
, 'Random Sample (Random Sample (builtup)) [Cyan] 500 points}' };
lup = permute( reshape( [ lookup{:} ], 3, [] ), [ 2, 1 ] );
cac = regexp( names, '\<([\w ]+)(?:\[|\(.+){0,1}\>', 'tokens', 'once' );
cac = strtrim( cac );
str = cellfun( @(c) char(c), cac, 'uni', false );
I failed 'builtup':( Didn't read carefully enough.
  댓글 수: 1
Sukuchha
Sukuchha 2012년 4월 30일
Thanks it worked like a charm. need to learn more about regexp :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2012년 4월 30일
N = regexp(names,'\w*(?=(.{1,3}\[))','match')';
tst = ~cellfun('isempty',N);
clr = reshape(lookup,3,[])';
out = [cellfun(@(x)x{:},N(tst),'un',0) clr(tst,:)];

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by