How to acess data if its in cell form?

조회 수: 2(최근 30일)
riki singh
riki singh 2022년 3월 8일
댓글: Voss 2022년 3월 9일
name Age Salary Gender Vote
patient1 25 20000 M 1
patient2 45 15000 M 0
patient3 12 25000 F 0
T = table({'patient1';'patient2';'patient3'},[25;45;12],[5000;8000;8000],['M';'M';'F'],logical([1;0;0]),...
'VariableNames',{'Name','Age',','charge','gender','vote'});
T1=table2cell(T);
for i=1:3
for j=1:5
matrix{j}=T1(i,j); %assigning first row only in a loop
end
output1=matrix{j-2}; %assigning the Salary value i.e my output should be output1=20000 only (not entire column of salary)
output2=matrix{j-1}; %assigning gender value i.e my output should be output1=M only (not entire column of Gender)
if T1(i,5)==true
Gender_of_person=output2 %error cannot assign cell value
disp name of patient
else
end
  댓글 수: 1
riki singh
riki singh 2022년 3월 8일
for more clarity
my loop first should select first row from the table in that assign
output1=5000
output2='M'
if T(i,5)==true %logical one
then
Gender_of_person=output2
disp name of patient
else
end
then same procedure i have to follow for output2 also
if T(i,5)== true %logical one
then
Salary_of_person=output1 %getting error cannot assign cell value
disp name of patient
else
end

댓글을 달려면 로그인하십시오.

답변(2개)

Voss
Voss 2022년 3월 8일
Use curly braces { } to access the contents of tables
T = table( ...
{'patient1';'patient2';'patient3'}, ...
[25;45;12], ...
[5000;8000;8000], ...
['M';'M';'F'], ...
logical([1;0;0]),...
'VariableNames',{'Name','Age','charge','gender','vote'});
T1=table2cell(T);
for i=1:3
for j=1:5
matrix{j}=T1{i,j}; %assigning first row only in a loop
end
disp(matrix);
output1=matrix{j-2}; %assigning the Salary value i.e my output should be output1=20000 only (not entire column of salary)
output2=matrix{j-1}; %assigning gender value i.e my output should be output1=M only (not entire column of Gender)
if T1{i,5}==true
Gender_of_person=output2 %error cannot assign cell value
% disp name of patient
else
end
end
{'patient1'} {[25]} {[5000]} {'M'} {[1]}
Gender_of_person = 'M'
{'patient2'} {[45]} {[8000]} {'M'} {[0]} {'patient3'} {[12]} {[8000]} {'F'} {[0]}
  댓글 수: 2
Voss
Voss 2022년 3월 9일
% main program:
T = table({'ravi';'karan';'ramya'},[25;45;12],[5000;8000;8000],['M';'M';'F'],logical([1;0;0]),'VariableNames',{'Name','Age','charge','gender','vote'});
T1=table2cell(T);
for i=1:3
for j=1:5
matrix{j}=T1{i,j}; %assigning first row only in this loop
end
% disp(matrix);
task(matrix); %calling function
end
ravi 60000 karan 96000 ramya 96000
% function:
function task(matrix)
disp(matrix{1}); %display name of patient
disp(matrix{3}*12);
end

댓글을 달려면 로그인하십시오.


Peter Perkins
Peter Perkins 2022년 3월 9일
I really recommend you read my reply there, and explain what your end goal is, showing clearly what you are starting from and what you want to end up with. Not just code snippets.

Community Treasure Hunt

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

Start Hunting!

Translated by