How to acess data if its in cell form?
Show older comments
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
on 8 Mar 2022
Answers (2)
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
2 Comments
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);
task(matrix); %calling function
end
% function:
function task(matrix)
disp(matrix{1}); %display name of patient
disp(matrix{3}*12);
end
Peter Perkins
on 9 Mar 2022
0 votes
This looks susp[iciously like another post: https://www.mathworks.com/matlabcentral/answers/1664149-i-have-a-list-of-data-and-i-have-to-select-first-5th-column-of-each-row-then-simultaneously-6th-co
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 Loops and Conditional Statements 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!