How to acess data if its in cell form?

2 views (last 30 days)
riki singh
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
  1 Comment
riki singh
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

Sign in to comment.

Answers (2)

Voss
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 Comments
riki singh
riki singh 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);
output1=matrix{j-2}; % output should be output1=5000
output2=matrix{j-1}; % output should be output2='M'
if T1{i,5}==true %checking equality condition of data which is present in column 5
salary=output1 ; % assign salary=5000
task(salary) %calling function
else
end
end
function:
function task(salary)
for k=1:3 % i am getting error ..its not giving correct values
disp(matrix{j,1}) %display name of patient
salary=salary*12;
disp(salary)
end
end
my output should be:
in first loop :ravi in second loop :karan in third loop :ramya
60000 96000 96000
what commands i should write so that i can access table first column data (name of patient)of main program in every loop which i have written in function
Voss
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);
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

Sign in to comment.


Peter Perkins
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.

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!