How to acess data if its in cell form?

조회 수: 1 (최근 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
riki singh
riki singh 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);
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 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.

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by