# How to acess data if its in cell form?

1 view (last 30 days)
riki singh on 8 Mar 2022
Commented: Voss on 9 Mar 2022
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
riki singh on 8 Mar 2022
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

Voss on 8 Mar 2022
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 CommentsShowHide 1 older comment
Voss on 9 Mar 2022
% 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);
end
ravi 60000 karan 96000 ramya 96000
% function:
disp(matrix{1}); %display name of patient
disp(matrix{3}*12);
end

Peter Perkins on 9 Mar 2022
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.