what is wrong in my code..?I am not able to repeat result for all last names..what i should edit in my code

3 Ansichten (letzte 30 Tage)
LastName = {'Sanchez';'Johnson';'Li';'Diaz'};
Age = [38;43;38;40];
Smoker = [10;20;30;60];
indicator1 = logical([0;0;1;1]);
indicator2 = logical([0;1;0;1]);
BP1 = [93; 77; 83;80];
BP2=[125;135;146;150];
T = table(LastName,Age,Smoker,indicator1,indicator2,BP1,BP2); T1=table2cell(T);
T2=T1(:,2:7);
matrix =cell(size(T1,1),1);
for i=1:4
for j=1:4
person_data=T2(i,j);
end
temp1=T2(i,3);
temp2=T2(i,4); %%indicator 2 selected
temp3=T2(i,5) ; %% here BP1 is selected
temp4=T2(i,6); %% here BP2 is selected medical(temp1,temp2,temp3,temp4)
end
function medical(temp1,temp2,temp3,temp4)
if indicator1==0
disp(BP1)
elseif indicator1==1
for k=1:4
result=[20;40;80;60]; %output is getting only for 80 and 60
r = result(k);
dept={('cardio');('neuro');('mediciene');(surgery')} ;
d=dept(k) ;
disp('dept')
disp(d)
disp(r)
end
end
if indicator2==1
BP=BP1+BP2;
disp(BP)
end
end
  4 Kommentare
Image Analyst
Image Analyst am 15 Apr. 2022
Bearbeitet: Image Analyst am 15 Apr. 2022
Save T1 and T2
save('answers.mat', 'T1', 'T2');
and then attach the mat file with the paperclip icon, after reading this:

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Kevin Holly
Kevin Holly am 15 Apr. 2022
Bearbeitet: Kevin Holly am 15 Apr. 2022
LastName = {'Sanchez';'Johnson';'Li';'Diaz'};
Age = [38;43;38;40];
Smoker = [10;20;30;60];
indicator1 = logical([0;0;1;1]); %%what changes should make here so that for
indicator2 = logical([0;1;0;1]); %%all last names i should get all result values
BP1 = [93; 77; 83;80];
BP2=[125;135;146;150]
BP2 = 4×1
125 135 146 150
T = table(LastName,Age,Smoker,indicator1,indicator2,BP1,BP2)
T = 4×7 table
LastName Age Smoker indicator1 indicator2 BP1 BP2 ___________ ___ ______ __________ __________ ___ ___ {'Sanchez'} 38 10 false false 93 125 {'Johnson'} 43 20 false true 77 135 {'Li' } 38 30 true false 83 146 {'Diaz' } 40 60 true true 80 150
T1=table2cell(T)
T1 = 4×7 cell array
{'Sanchez'} {[38]} {[10]} {[0]} {[0]} {[93]} {[125]} {'Johnson'} {[43]} {[20]} {[0]} {[1]} {[77]} {[135]} {'Li' } {[38]} {[30]} {[1]} {[0]} {[83]} {[146]} {'Diaz' } {[40]} {[60]} {[1]} {[1]} {[80]} {[150]}
T2=T1(:,2:7)
T2 = 4×6 cell array
{[38]} {[10]} {[0]} {[0]} {[93]} {[125]} {[43]} {[20]} {[0]} {[1]} {[77]} {[135]} {[38]} {[30]} {[1]} {[0]} {[83]} {[146]} {[40]} {[60]} {[1]} {[1]} {[80]} {[150]}
matrix =cell(size(T1,1),1);
for i=1:4
for j=1:4
person_data=T2(i,j);
end
temp1=T2(i,3);
temp2=T2(i,4); %%indicator 2 selected
temp3=T2(i,5); %% here BP1 is selected
temp4=T2(i,6); %% here BP2 is selected
medical(temp1,temp2,temp3,temp4,indicator1(i),indicator2(i),BP1(i),BP2(i))% Added indicator1, indicator2, BP1, and BP2, so these variables will be in the function's workspace
end
93 77 212 dept {'cardio'} 20 dept {'neuro'} 40 dept {'medicine'} 80 dept {'surgery'} 60 dept {'cardio'} 20 dept {'neuro'} 40 dept {'medicine'} 80 dept {'surgery'} 60 230
function medical(temp1,temp2,temp3,temp4,indicator1,indicator2,BP1,BP2) % Added indicator1, indicator2, BP1, and BP2, so these variables will be in the function's workspace
if indicator1==0
disp(BP1)
elseif indicator1==1
for k=1:4
result=[20;40;80;60]; %output is getting only for 80 and 60 values
r = result(k);
dept={('cardio');('neuro');('medicine');('surgery')};
d=dept(k);
disp('dept')
disp(d)
disp(r)
end
end
if indicator2==1
BP=BP1+BP2;
disp(BP)
end
end
  2 Kommentare
ramya
ramya am 15 Apr. 2022
Sir for all lastnames i need required output ie ##when Indicator1==0 Sanchez 93 Johnson 77 ##indicator1==1 Li dept {'cardio'} 20 dept {'neuro'} 40 dept {'medicine'}80 dept {'surgery'} 60
Diaz dept {'cardio'} 20 dept {'neuro'} 40 dept {'medicine'}80 dept {'surgery'} 60
##whenindicator2==1 Johnson BP 212
Diaz. BP 230
Kevin Holly
Kevin Holly am 15 Apr. 2022
Bearbeitet: Kevin Holly am 15 Apr. 2022
Here is an alternative without the temp1,temp2... variables and cleaner display.
Edit: I included the last names and removed unnecessary variables.
LastName = {'Sanchez';'Johnson';'Li';'Diaz'};
Age = [38;43;38;40];
Smoker = [10;20;30;60];
indicator1 = logical([0;0;1;1]); %%what changes should make here so that for
indicator2 = logical([0;1;0;1]); %%all last names i should get all result values
BP1 = [93; 77; 83;80];
BP2=[125;135;146;150];
T = table(LastName,Age,Smoker,indicator1,indicator2,BP1,BP2)
T = 4×7 table
LastName Age Smoker indicator1 indicator2 BP1 BP2 ___________ ___ ______ __________ __________ ___ ___ {'Sanchez'} 38 10 false false 93 125 {'Johnson'} 43 20 false true 77 135 {'Li' } 38 30 true false 83 146 {'Diaz' } 40 60 true true 80 150
for i=1:size(T,1)
medical(T.indicator1(i),T.indicator2(i),BP1(i),T.BP2(i),T.LastName{i})% Added indicator1, indicator2, BP1, and BP2, so these variables will be in the function's workspace
end
Sanchez BP 93 Johnson BP 77 Johnson BP 212 Li dept cardio 20 dept neuro 40 dept medicine 80 dept surgery 60 Li dept {'cardio'} 20 dept {'neuro'} 40 dept {'medicine'} 80 dept {'surgery'} 60 Diaz dept cardio 20 dept neuro 40 dept medicine 80 dept surgery 60 Diaz dept {'cardio'} 20 dept {'neuro'} 40 dept {'medicine'} 80 dept {'surgery'} 60 Diaz BP 230
function medical(indicator1,indicator2,BP1,BP2,LastName) % Added indicator1, indicator2, BP1, and BP2, so these variables will be in the function's workspace
result=[20;40;80;60]; %output is getting only for 80 and 60 values
dept=["cardio";"neuro";"medicine";"surgery"];
if indicator1==false
disp([LastName ' BP ' num2str(BP1)])
elseif indicator1==true
Phrase = LastName;
Phrase2 = LastName;
for k=1:length(dept)
Phrase = [Phrase ' dept ' char(dept(k)) ' ' num2str(result(k))];
Phrase2 = [Phrase2 ' dept {''' char(dept(k)) '''} ' num2str(result(k))]; %This one give what you wrote above
end
disp(Phrase)
disp(Phrase2)
end
if indicator2==true
BP=BP1+BP2;
disp([LastName ' BP ' num2str(BP)])
end
end

Melden Sie sich an, um zu kommentieren.


Voss
Voss am 15 Apr. 2022
There seems to be some confusion about how functions work in MATLAB, so you may benefit from going through this: https://www.mathworks.com/learn/tutorials/matlab-onramp.html
I made modifications that I think are consistent with what your code is supposed to do. See my comments in the code for some explanation:
LastName = {'Sanchez';'Johnson';'Li';'Diaz'};
Age = [38;43;38;40];
Smoker = [10;20;30;60];
% close parenthesis ) was missing here:
indicator1 = logical([0;0;1;1]); %%what changes should make here so that for
% and here:
indicator2 = logical([0;1;0;1]); %%all last names i should get all result values
BP1 = [93; 77; 83;80];
BP2=[125;135;146;150]
BP2 = 4×1
125 135 146 150
T = table(LastName,Age,Smoker,indicator1,indicator2,BP1,BP2)
T = 4×7 table
LastName Age Smoker indicator1 indicator2 BP1 BP2 ___________ ___ ______ __________ __________ ___ ___ {'Sanchez'} 38 10 false false 93 125 {'Johnson'} 43 20 false true 77 135 {'Li' } 38 30 true false 83 146 {'Diaz' } 40 60 true true 80 150
T1=table2cell(T)
T1 = 4×7 cell array
{'Sanchez'} {[38]} {[10]} {[0]} {[0]} {[93]} {[125]} {'Johnson'} {[43]} {[20]} {[0]} {[1]} {[77]} {[135]} {'Li' } {[38]} {[30]} {[1]} {[0]} {[83]} {[146]} {'Diaz' } {[40]} {[60]} {[1]} {[1]} {[80]} {[150]}
T2=T1(:,2:7)
T2 = 4×6 cell array
{[38]} {[10]} {[0]} {[0]} {[93]} {[125]} {[43]} {[20]} {[0]} {[1]} {[77]} {[135]} {[38]} {[30]} {[1]} {[0]} {[83]} {[146]} {[40]} {[60]} {[1]} {[1]} {[80]} {[150]}
% undefined 'a1' error (but 'matrix' is unused, so I comment this line out):
% matrix =cell(size(a1,1),1);
for i=1:4
% this loop serves no purpose:
% for j=1:4
% person_data=T2(i,j);
% end
temp1=T2{i,3}
temp2=T2{i,4} %%indicator 2 selected
temp3=T2{i,5} %% here BP1 is selected
temp4=T2{i,6} %% here BP2 is selected
medical(temp1,temp2,temp3,temp4)
end
temp1 = logical
0
temp2 = logical
0
temp3 = 93
temp4 = 125
93
temp1 = logical
0
temp2 = logical
1
temp3 = 77
temp4 = 135
77 212
temp1 = logical
1
temp2 = logical
0
temp3 = 83
temp4 = 146
dept = 4×1 cell array
{'cardio' } {'neuro' } {'mediciene'} {'surgery' }
d = 1×1 cell array
{'cardio'}
dept {'cardio'} 20
dept = 4×1 cell array
{'cardio' } {'neuro' } {'mediciene'} {'surgery' }
d = 1×1 cell array
{'neuro'}
dept {'neuro'} 40
dept = 4×1 cell array
{'cardio' } {'neuro' } {'mediciene'} {'surgery' }
d = 1×1 cell array
{'mediciene'}
dept {'mediciene'} 80
dept = 4×1 cell array
{'cardio' } {'neuro' } {'mediciene'} {'surgery' }
d = 1×1 cell array
{'surgery'}
dept {'surgery'} 60
temp1 = logical
1
temp2 = logical
1
temp3 = 80
temp4 = 150
dept = 4×1 cell array
{'cardio' } {'neuro' } {'mediciene'} {'surgery' }
d = 1×1 cell array
{'cardio'}
dept {'cardio'} 20
dept = 4×1 cell array
{'cardio' } {'neuro' } {'mediciene'} {'surgery' }
d = 1×1 cell array
{'neuro'}
dept {'neuro'} 40
dept = 4×1 cell array
{'cardio' } {'neuro' } {'mediciene'} {'surgery' }
d = 1×1 cell array
{'mediciene'}
dept {'mediciene'} 80
dept = 4×1 cell array
{'cardio' } {'neuro' } {'mediciene'} {'surgery' }
d = 1×1 cell array
{'surgery'}
dept {'surgery'} 60 230
function medical(temp1,temp2,temp3,temp4)
% Unrecognized function or variable 'indicator1' here, but temp1 here is
% indicator1 in the calling function, so I assume that's
% what is meant (same for indicator2/temp2)
% (except that temp1 and temp2 are cell arrays, so I change the calling
% function to send the contents of the cells to 'medical' instead of
% scalar cell arrays)
if temp1==0
% disp(BP1) % BP1 is temp3
disp(temp3)
elseif temp1==1
for k=1:4
result=[20;40;80;60]; %output is getting only for 80 and 60 values
r = result(k);
dept={('cardio');('neuro');('mediciene');('surgery')} % "surgery'" I assume was a typo
d=dept(k)
disp('dept')
disp(d)
disp(r)
end
end
if temp2==1
% BP=BP1+BP2; % BP2 is temp4
BP = temp3+temp4;
disp(BP)
end
end
  5 Kommentare
Voss
Voss am 18 Apr. 2022
The error is from that software, not from MATLAB. Read the documentation for that software to figure out what to do.
Also note that the code in your original question did not use that software, and that myself and others have answered that question. Perhaps you should make a new question about how to interact with that software and someone familiar with doing so will answer it.

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by