필터 지우기
필터 지우기

how to read a .txt file in matlab ?

조회 수: 3 (최근 30일)
preet
preet 2013년 7월 4일
i want to read a file name is imData.txt which has 9600 arbitrary float numbers. and store in array of size 80*120 after read i want show image of that array??
  댓글 수: 6
preet
preet 2013년 7월 6일
walter there 120 float values in a row.and total 80 rows. 2nd location is (1,2).
every value delimited by space dear. min value is 0.000000 and max is 619.420000
preet
preet 2013년 7월 6일
@walter , values are in double or say float . but image in MATLAB will shown after conversion in uint8 wt should i do?

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

채택된 답변

Walter Roberson
Walter Roberson 2013년 7월 6일
For data in that format, load() should work, as would textscan(). You would not need to reshape() the data after either of those.
image() and imagesc() do not convert to uint8. However, when image() is passed floating point data (instead of uint8 or uint16) then image will expect the data to be in the range of 0 to 1, whereas imagesc() would rescale the floating point values so that the minimum mapped to 0 and the maximum mapped to 1.
What you should do about the fact your data ranges from 0 to 619 depends on your purposes. Are you okay with the loss of resolution if you rescale to 0 to 255? If so then
scaleddata = uint8(YourData ./ max(YourData(:)) .* 255);
Or you could leave the data in floating point format if you do not specifically need it to be uint8()
scaleddata = YourData ./ max(YourData(:);
The above assumes that for each file, the maximum value of the file should be treated as being full brightness. If there is some other value that should be treated as maximum brightness (e.g., 640) then use that value in place of max(YourData(:))

추가 답변 (2개)

Sivakumaran Chandrasekaran
Sivakumaran Chandrasekaran 2013년 7월 4일
use textread command
  댓글 수: 3
Jan
Jan 2013년 7월 5일
@kp: Then please show what you have tried and explain the problem. Currently too much guessing is required to give an answer.
preet
preet 2013년 7월 6일
@ jan please read my previous comment after walter's comment, for explanation.

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


Hugo
Hugo 2013년 7월 6일
Suppose that your file is called data.txt You can load the data simply with
load('data.txt');
which will create a variable called "data"
and the print it using
imagesc(data)
  댓글 수: 4
preet
preet 2013년 7월 6일
but my values are in double but images will be shown after convrt in uint8 so wts the solution?
Hugo
Hugo 2013년 7월 6일
If I understood right, you said that the values are in matrix form in the file. That should mean that your file is structures as lines ending in a newline symbol, and each number in each line is separated by a space. If that's the case, no matter whether the numbers are integer or real, load(...) will give you a matrix with as many rows as lines in the file and as many columns as numbers in the line, provided the number of numbers is the same in each line.
Once this is solve, I don't know what your values represent. If the position in the matrix represents the position of the pixels and the value represents the colour, then image(...) should show the image. Otherwise, I think it's up to you what you really have and how you really want to represent it.
I hope this helps you. Good luck.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by