How to present the content of a field in a program?
조회 수: 4 (최근 30일)
이전 댓글 표시
I want to present the variables and their value from a structure in a program, just for information for the users.
Let's say:
a.a=234.5;
a.b=444;
a.s='This is an example';
a
if I run this code in the command window, I get the answer:
a =
struct with fields:
a: 234.5
b: 444
s: 'This is an example'
I want to reproduce this answer and put it in a text window in my program. I could not figure out how to do that.
Thanks for any help!
댓글 수: 0
채택된 답변
Image Analyst
2025년 5월 31일
편집: Image Analyst
2025년 5월 31일
Try this:
a.a=234.5;
a.b=444;
a.s='This is an example';
a
fn = fieldnames(a)
str = sprintf('a = \nstruct with fields:');
for k = 1 : numel(fn)
thisFieldName = fn{k};
if ischar(a.(thisFieldName))
% Print using %s
str = sprintf("%s\n\t%s: '%s'", str, thisFieldName, a.(thisFieldName));
else
% Print using %g
str = sprintf("%s\n\t%s: %g", str, thisFieldName, a.(thisFieldName));
end
end
str % Display in command window.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!