File Exchange

image thumbnail

Show

version 1.0.0.0 (7.55 KB) by Joseph Kirk
Prints the contents of structures, including structure arrays and nested structures

0 Downloads

Updated 06 Oct 2015

View License

SHOW Prints the full contents of a multi-element or array structure

Description: Prints the full contents of a structure variable to the Command
Window or to a file. Ideal for showing contents of a nested
structure or a structure array.

Motivation:
Often it takes multiple commands to explore the contents of a structure,
especially if it is a structure array or has nested structure elements.
For example, take this relatively simple case:
>> s = struct('data',{struct('value',{0,'a',pi}),'str',magic(3),1-2i})
This structure is displayed by MATLAB as:
s =
1x4 struct array with fields:
data
Not much information there, other than the top level size and field(s).
To explore the actual contents, you would need to type:
>> s(1)
>> s(1).data(1)
>> s(1).data(2)
>> s(1).data(3)
>> s(2)
>> s(3)
>> s(4)
With show, all that effort is done for you in one simple command:
>> show(s)
Also, the printed output is more readable and concise, with field names
tab-indented to represent nested structure elements.

Inputs:
VAR - (MxN struct) variable to be shown
FID - (optional) file identifier to print contents to a file

Outputs: None.

Usage:
show(var)
-or-
fid = fopen('file.txt','w'); show(var,fid); fclose(fid);

Example:
% Show full contents of the image info (needs Image Processing Toolbox)
structInfo = imfinfo('cloudCombined.jpg');
show(structInfo);

Example:
clc
myStruct = struct('nestedStruct',struct('version','1.0','size',[800 600], ...
'path','/home/user/matlab','file','data.mat', 'matrixData',magic(3), ...
'img',uint8(rand(600,800,3)),'cellField',{{'a','b','c'}}, ...
'shortStringField','A string with 100 characters or less is printed', ...
'longStringField',repmat('Only size/type shown for long strings. ',1,3), ...
'rowVectorData',rand(1,3),'columnVectorData',sort(rand(5,1))), ...
'structArray',struct('data',{[1+4i,3-2i],randperm(10),struct('val',{'a',2,pi})}))
fprintf('\nCompare default MATLAB "disp" above versus "show" below:\n\n');
show(myStruct);

Example:
% Print contents to a file
[uv,sv] = memory;
memoryInfo = struct('UserView',uv,'SystemView',sv);
fid = fopen('memory.txt','w');
show(memoryInfo,fid);
fclose(fid);

Example:
% Print contents to the screen
[uv,sv] = memory;
memoryInfo = struct('UserView',uv,'SystemView',sv);
show(memoryInfo);

Author: Joseph Kirk
Email: jdkirk630@gmail.com
Release: 1.0
Release Date: 10/5/15

Comments and Ratings (0)

Updates

1.0.0.0

Added screen shot.

MATLAB Release Compatibility
Created with R2015b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor