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)
Show older comments
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
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.
Answers (2)
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
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
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."?
See Also
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!