필터 지우기
필터 지우기

how to print the data that we collect using structure and loop

조회 수: 2 (최근 30일)
Prateek Ghorawat
Prateek Ghorawat 2021년 7월 1일
댓글: Yongjian Feng 2021년 7월 1일
function details = my_details()
x=input('Enter number to details needed to be entered : ');
for m=1:x
name=input('name ','s');
age=input('age ');
contact_number=input('contact number ');
details=struct('name',name,'age',age,'contact',contact_number);
end
end

답변 (2개)

Yongjian Feng
Yongjian Feng 2021년 7월 1일
Hello Prateek,
A simple way to print out the struct. Just get rid of the ';' at the end of the line.
function details = my_details()
x=input('Enter number to details needed to be entered : ');
for m=1:x
name=input('name ','s');
age=input('age ');
contact_number=input('contact number ');
% remove ';' at the end, command line window will print this
details=struct('name',name,'age',age,'contact',contact_number)
end
end
Thanks,
Yongjian
  댓글 수: 2
Prateek Ghorawat
Prateek Ghorawat 2021년 7월 1일
now its giving data as i am entering but i want to show data once i enter all
Yongjian Feng
Yongjian Feng 2021년 7월 1일
Then you need to store the data first into an array of structs.
Once you get all the inputs, iterate the array of structs, and do the same thing here (a line without ';').

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


Mili Goyal
Mili Goyal 2021년 7월 1일
편집: Mili Goyal 2021년 7월 1일
I think in the above code, details is storing the last struct. To store all the structs, we can make details as an array of structs and finally print that. The following is the modified code for the above which stores the structs in an array and finally prints them:
CODE
--------------------------------------------------------------------------------------------------------------------------------------------
function details = my_details()
details = [];
x=input('Enter number to details needed to be entered : ');
for m=1:x
name=input('name ','s');
age=input('age ');
contact_number=input('contact number ');
details= [details struct('name',name,'age',age,'contact',contact_number)];
end
for i = 1:length(details) % print the details
disp(['The details of entry number ' int2str(i)]);
disp(['name :' details(i).name]);
disp(['age :' int2str(details(i).age)]);
disp(['contact :' int2str(details(i).contact)]);
end
end
-------------------------------------------------------------------------------------------------------------------------
Hope this helps!

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by