MATLAB Answers

Error in my function in my for loop

1 view (last 30 days)
Hello I am trying to make a function. I have for inputs datetime 3 of them one of them is logical. I am trying to calcutate a number from many rows. I have for loop, my max iteration is the height of my table. I have cases for logical data part of my function to decide the final output, I am unable to put the final answer in an order as output. I am getting an error with exceeding matrix dimensions.
function [Count SensoredData] = sensor(StartDate,EndDate,PatientinResearch,LogicalDeath)
% X is start of the research.
% Y end date of the Research.
% E is number of days patient stay in the research
% Z is if death is occured or not.
if ~isdatetime(StartDate)
error('MyComponent:incorrectType',...
'Error. \nInput must be a DateTime for StartDate, not a %s.',class(StartDate))
end
if ~isdatetime(EndDate)
error('MyComponent:incorrectType',...
'Error. \nInput must be a DateTime for EndDate, not a %s.',class(EndDate))
end
if ~isdatetime(PatientinResearch)
error('MyComponent:incorrectType',...
'Error. \nInput must be a DateTime for PatientinResearch, not a %s.',class(PatientinResearch))
end
if ~isa(LogicalDeath,'logical')
error('MyComponent:incorrectType',...
'Error. \nInput must be a Logical for LogicalDeath, not a %s.',class(LogicalDeath))
end
X = StartDate;
Y = EndDate;
E = PatientinResearch;
Z = LogicalDeath;
T = table();
% totalTimeOfResearch = caldays(between(X,Y,'days'));
%
% sdn1 = datenum( Y );
% sdn2 = datenum( E );
% format long
%
[n,m] = size(X);
for i = 1 : 1 : n
if caldays(between(X,Y,'days')) >= caldays(between(X,E,'days'))
switch Z
case 0
p = 0;
T(i,:) = [i p];
case 1
p = 1;
T(i,:) = [i p];
end
end
if caldays(between(X,Y,'days')) < caldays(between(X,E,'days'))
switch Z
case 0
p = 1;
T(i,:) = [i p];
case 1
p = 0;
T(i,:) = [i p];
end
end
end
Count = T(:,1);
SensoredData = T(:,2);
end

  0 Comments

Sign in to comment.

Accepted Answer

Jesús Zambrano
Jesús Zambrano on 7 Oct 2020
Try to define the size of T before using it. Also, make sure that the line of code
[n,m] = size(X);
is giving you the value of n you expect.

  3 Comments

Ongun Palaoglu
Ongun Palaoglu on 8 Oct 2020
i have modified the code a little, I added size of table and disp values to see what went wrong. Now I am getting a table but its all 0s and I cannot see anyother disp under switch case functions.
function SensoredData = sensor(StartDate,EndDate,PatientinResearch,LogicalDeath)
% X is start of the research.
% Y end date of the Research.
% E is number of days patient stay in the research
% Z is if death is occured or not.
if ~isdatetime(StartDate)
error('MyComponent:incorrectType',...
'Error. \nInput must be a DateTime for StartDate, not a %s.',class(StartDate))
end
if ~isdatetime(EndDate)
error('MyComponent:incorrectType',...
'Error. \nInput must be a DateTime for EndDate, not a %s.',class(EndDate))
end
if ~isdatetime(PatientinResearch)
error('MyComponent:incorrectType',...
'Error. \nInput must be a DateTime for PatientinResearch, not a %s.',class(PatientinResearch))
end
if ~isa(LogicalDeath,'logical')
error('MyComponent:incorrectType',...
'Error. \nInput must be a Logical for LogicalDeath, not a %s.',class(LogicalDeath))
end
X = StartDate;
Y = EndDate;
E = PatientinResearch;
Z = LogicalDeath;
n = numel(X);
T = table('Size', [n 1],'VariableTypes', "logical");
disp('h')
% totalTimeOfResearch = caldays(between(X,Y,'days'));
%
% sdn1 = datenum( Y );
% sdn2 = datenum( E );
% format long
%
for i = 1 : 1 : n
if caldays(between(X,Y,'days')) >= caldays(between(X,E,'days'))
disp('g')
switch Z
case 0
p = 0;
T(i) = p;
disp('a')
case 1
p = 1;
T(i) = p;
disp('b')
end
end
if caldays(between(X,Y,'days')) < caldays(between(X,E,'days'))
disp('f')
switch Z
case 0
p = 1;
T(i) = p;
disp('c')
case 1
p = 0;
T(i) = p;
disp('d')
end
end
SensoredData(i,1) = T(i,1);
end
end
Seth Furman
Seth Furman on 8 Oct 2020
I'm not clear on the goal of this code, but here are a few common pitfalls I notice:
Access Data in Tables
  • Indexing into a table always requires 2 indices, so the line "T(i) = p;" will always fail. Instead you want "T(i,1) = p;".
  • When assigning a non-table into a table, use curly braces instead of smooth parentheses, for example "T{i,1} = p; % OK" instead of "T(i,1) = p; % ERRORS if p isn't a table".
Prefer Vectorization
  • Use vectorization where possible because it's typically faster and leads to shorter code.
  • One example where you can add vectorization in this case is by creating SensoredData at the end of your function, instead of inside the for-loop with a simple index into T, for example "SensoredData = T(:,1);"
Ongun Palaoglu
Ongun Palaoglu on 12 Oct 2020
Somehow I solved it, probably it is due to missing date lines

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by