How can I create a variable number of arrays and plot in the same image?

조회 수: 3 (최근 30일)
Ted Baker
Ted Baker 2020년 7월 16일
댓글: Ted Baker 2020년 7월 16일
Hi,
I'm trying to plot some txt files which have two columns, 'a' and 'b' as an image. 'a' is a variable that always counts from 0 to some number 'n', repeatedly 'k' number of times. 'b' is a value I'd like to assign to a pixel in the image. I've included a sample file to try and demonstrate this, where n = 4 and k = 3.
Ideally I would like to plot an image such that the number 'b' varies the colour of the pixel, with the x coordinate of the pixel determined by the value of 'a' and the y cordinated determined by the cycle through 'a', up to 'k'. Even writing this is a little confusing, so I hope this diagram will clarify how I would like the data to be plotted (using the example.txt file, where i've substituted the colour for just the value 'b').
However, I'm very confused as to how I can firstly calculated the number of times 'a' cycles (k), and secondly how I could either create one large array of size 'n' by 'k' to feed into the image() function. Is there a fast way of doing this, without needing to alter the code every runtime? My actual files are very large and I have hundreds of files, each with different 'n' and 'k' values - would I need some dynamic to accomodate this?
Thanks in advance.

채택된 답변

jonas
jonas 2020년 7월 16일
편집: jonas 2020년 7월 16일
Something like this?
data = readmatrix('example.txt')
x = data(:,1);
v = data(:,2);
id = find(x == 0);
im = reshape(v,id(2)-1,[])'
image(im)
  댓글 수: 1
Ted Baker
Ted Baker 2020년 7월 16일
Thank you jonas, that was perfect. I see how I can used find() in the future with reshape(). Smart. :)

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

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 7월 16일

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by