what is wrong in my code..?I am not able to repeat result for all last names..what i should edit in my code
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2022-4-15
编辑:Image Analyst
2022-4-15
Save T1 and T2
save('answers.mat', 'T1', 'T2');
and then attach the mat file with the paperclip icon, after reading this:
回答(2 个)
Kevin Holly
2022-4-15
编辑:Kevin Holly
2022-4-15
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)
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,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
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 个评论
Kevin Holly
2022-4-15
编辑:Kevin Holly
2022-4-15
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)
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
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
Voss
2022-4-15
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]
T = table(LastName,Age,Smoker,indicator1,indicator2,BP1,BP2)
T1=table2cell(T)
T2=T1(:,2:7)
% 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
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 个评论
Voss
2022-4-18
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!