Code not working when putting in 1:length(X), but code is working when putting the value of length(X)

4 views (last 30 days)
Hi,
I've situation, and I really don't what the reason for this error is. I want to define a cells in a cell. Herefore a made a for-loop in a for-loop So I have the following code:
processed_segment = cell(3,1);
processed_stage = cell(1,6);
for stage = 1:6
stage_patient = patientsignal{1,stage};
length_stage = length(stage_patient);
numberOfSegment = int16(length_stage/Nsegment);
processed_signal = cell(1, numberOfSegment-1);
for z = 1:73
[iECG,fECG,SQI, segmentpatient] = FINALECG_Stage(patientdata, patientnumber, stage ,Nsegment,Fs,Measurement, z);
processed_segment{1,1} = iECG;
processed_segment{2,1} = fECG;
processed_segment{3,1} = SQI;
processed_signal{1,z} = processed_segment;
segmentpatient = segmentpatient;
end
processed_stage{1, stage} = processed_signal;
end
In the second for-loop you see z variable.
When I use " z = 1 : 73 ", the code is working fine and its executes the loop as wished.
But when I use " z = 1 : numberOfSegment ", and in this case the value of numberOfSegment is 73, I get the following error:
Error using welchparse>segment_info (line 258)
The length of the segments cannot be greater than the length of the input signal.
..... just why tho? What is the reason?
  3 Comments
Enes Kinaci
Enes Kinaci on 15 May 2020
Each stage contains an array of " X (samples) row by 1 column". But the X samples differs per stage. That is why I calculate the length of the array for each stage. With:
stage_patient = patientsignal{1,stage};
length_stage = length(stage_patient);
Each stage is between 100.000 and 700.000 samples. Depending on the number of samples, the array is being segmented in arrays of 10.000 samples (Nsegment = 10.000).
With the following code, I calculate the number of segments that occurs based on the X samples.
numberOfSegment = int16(length_stage/Nsegment);
Suppose I have a stage which contains an array of (730.000 x 1). Then, the number of segments that I get is 73. I want to process and analyze each of this segment seperately. The second for-loop is used for selecting each segment seperately.
My problem is: If I write " for z = 1 : numberOfSegments ", the code is not working after 4 iterations. But when I put " z = 1 : 73 " the whole loop is being executed (73 iterations), and goes to the next stage (executing the first for-loop).
Ameer Hamza
Ameer Hamza on 15 May 2020
Such problems can easily be solved using breakpoints: https://www.mathworks.com/help/matlab/matlab_prog/set-breakpoints.html. Add a breakpoint at the line
for z = 1:numberOfSegment
from that point, run the code line by line. You will see which line causes an unexpected behavior causing the loop to terminate.

Sign in to comment.

Answers (2)

Ameer Hamza
Ameer Hamza on 15 May 2020
length() function call can be ambiguous and create the potential of such errors when used carelessly. Instead, use size(): https://www.mathworks.com/help/releases/R2020a/matlab/ref/size.html and explicitly specify the dimension.

Image Analyst
Image Analyst on 15 May 2020
It's probably because the patientsignal cell array does not have 73 cells in it. I've tried to put in some checks and make the code more robust below, but of course I can't test it because you didn't include data or the top part of your program.
processed_segment = cell(3,1); % 1-D column vector of cells.
processed_stage = cell(1,6); % 1-D row vector of cells.
for stage = 1 : 6
% Get the contents of the "stage" column of row 1 of patientsignal.
stage_patient = patientsignal{1,stage};
length_stage = length(stage_patient);
% Above line of code assumes stage_patient is a 1-D vector. Let's check to make sure.
[rows, columns] = size(stage_patient);
fprintf('stage_patient has %d rows and %d columns.\n', rows, columns);
if ~(rows == 1 || column == 1)
warningMessage = sprintf('Hey!\nstage_patient is not a vector -- it is a matrix.\nstage_patient has %d rows and %d columns.\n', rows, columns);
uiwait(warndlg(warningMessage));
end
numberOfSegments = int16(length_stage/Nsegment);
fprintf(' Stage %d has %d segments.\n', numberOfSegments);
processed_signal = cell(numberOfSegments); % 1-D vector of cells.
for z = 1 : numberOfSegments
[iECG, fECG, SQI, segmentpatient] = FINALECG_Stage(patientdata, patientnumber, stage ,Nsegment,Fs,Measurement, z);
processed_segment{1,1} = iECG;
processed_segment{2,1} = fECG;
processed_segment{3,1} = SQI;
processed_signal{1,z} = processed_segment;
end
processed_stage{1, stage} = processed_signal;
end
  2 Comments
Enes Kinaci
Enes Kinaci on 15 May 2020
Each stage contains an array of " X (samples) row by 1 column". But the X samples differs per stage. That is why I calculate the length of the array for each stage. With:
stage_patient = patientsignal{1,stage};
length_stage = length(stage_patient);
Each stage is between 100.000 and 700.000 samples. Depending on the number of samples, the array is being segmented in arrays of 10.000 samples (Nsegment = 10.000).
With the following code, I calculate the number of segments that occurs based on the X samples.
numberOfSegment = int16(length_stage/Nsegment);
Suppose I have a stage which contains an array of (730.000 x 1). Then, the number of segments that I get is 73. I want to process and analyze each of this segment seperately. The second for-loop is used for selecting each segment seperately.
My problem is: If I write " for z = 1 : numberOfSegments ", the code is not working after 4 iterations. But when I put " z = 1 : 73 " the whole loop is being executed (73 iterations), and goes to the next stage (executing the first for-loop).
Image Analyst
Image Analyst on 16 May 2020
Did you see where I said "of course I can't test it because you didn't include data or the top part of your program."?

Sign in to comment.

Categories

Find more on Categorical Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!