Plotting signal of a region of interest

1 view (last 30 days)
DM
DM on 5 Feb 2021
Commented: Steve Eddins on 5 Feb 2021
Hi,
I have 3D data [114,114,55] = [x, y , time]. I also have a binary mask with 0 background and 1s the pixels correspong to lung. I would like to plot the Signal versus time for the lung region.
I did the following:
dim=mask;
SignalROI = zeros(1,1, size(dim,3));
lung = lungdata.*mask;
for i=0, size(dim,3)
SignalROI(:,:,i) = mean(lung(:,:,i));
end
plot(SignalROI)
But I get the following error:
"Subscript indices must either be real positive integers or logicals."
Could you please help?

Answers (1)

Steve Eddins
Steve Eddins on 5 Feb 2021
Edited: Steve Eddins on 5 Feb 2021
A possible explanation is that you have a variable called "size" or "mean" in your workspace. For example, if you have a variable called "size" in your workspace, then size(dim,3) is an indexing expression into that variable rather than a call to the size function.
Here are a couple of other notes about the code:
  • The for-loop syntax is incorrect. Indexing should start at 1, and the syntax should be for i = 1:size(dim,3). (Starting the index at 0 would also produce the error message you received.)
  • mean(lung(:,:,i)) doesn't return a scalar. It returns a vector containing the mean of each column of lung(:,:,i). If you have R2018b or later, use this instead: mean(lung(:,:,i),'all'). Otherwise, use mean(mean(lung(:,:,i))).
  4 Comments
DM
DM on 5 Feb 2021
Sorry I didn't noticed that. I corrected the code as you suggested but then I has another error:
"Error using plot
Data cannot have more than 2 dimensions."
SignalROI = zeros(1,1, 26);
lung = lungdata.*mask;
for i=1:26
SignalROI(:,:,i) = mean(mean(lung(:,:,i)));
end
plot(SignalROI)
So I wrote the above code like this:
SignalROI = zeros(26);
lung = lungdata.*mask;
for i=1:26
SignalROI(i) = mean(mean(lung(:,:,i)));
end
plot(SignalROI)
Is the above correct?
Steve Eddins
Steve Eddins on 5 Feb 2021
Almost. zeros(26) makes a 26x26 matrix. Use zeros(26,1) or zeros(1,26) instead, depending on whether you want a column or a row vector.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!