whos - I can't capture the output
10 views (last 30 days)
Show older comments
I need to debug a script that has memory issues. So I decided to use whos to print out the largest variables while it is running.
The option to store the output in a cell array doesn't seem to work for the workspace, as illustrated by the following:
>> whos
Name Size Bytes Class Attributes
C 5x1 572 cell
S 7x1 9852 struct
ans 1x102 204 char
datimNow 1x20 40 char
mem 1x105 210 char
memory 1x81 162 char
status 1x1 8 double
>> S = whos()
S =
7×1 struct array with fields:
name
size
bytes
class
global
sparse
complex
nesting
persistent
Am I doing something wrong, or is there a better way to monitor variable space use during execution?
2 Comments
Dyuman Joshi
on 19 Jan 2024
Edited: Dyuman Joshi
on 19 Jan 2024
"The option to store the output in a cell array doesn't seem to work for the workspace"
Maybe I am misunderstanding, but the outptut is a struct array.
You can get the data from the size field, corresponding to the variables in the name field.
Edit - Note that calling whos() multiple times in your code will lead to reduction in code efficiency.
From this particular documentation page- "Avoid functions that query the state of MATLAB such as inputname, which, whos, exist(var), and dbstack. Run-time introspection is computationally expensive."
@dormant, How many times is whos() called in your code? And do you just have to see which variable takes the most bytes for storage?
Accepted Answer
Star Strider
on 19 Jan 2024
Perhaps something like this —
A = magic(7);
B = string(randi(9, 4));
C = sin(2*pi*(0:0.01:1));
whos
S = whos;
Fields = fieldnames(S);
Contents = struct2cell(S);
whos_result = [cell2table(Fields) cell2table(Contents)]
MemoryCell = whos_result(strcmp(whos_result.Fields,'bytes'), :)
Total_Variable_Memory_Used_Bytes = sum(cellfun(@sum, table2array(MemoryCell(:,2:end))))
.
2 Comments
Steven Lord
on 22 Jan 2024
Rather than convert the struct into multiple cell arrays and displaying one table variable per workspace variable, I'd display one table row per workspace variable instead.
A = magic(7);
B = string(randi(9, 4));
C = sin(2*pi*(0:0.01:1));
T = struct2table(whos)
Then you can call whatever functions you want on the variables.
areAnyVariablesSparse = any(T.sparse)
Note that simply summing up the bytes variable is not necessarily going to give you the total amount of memory used by MATLAB for those variables (copy-on-write, for example) but if you're looking for a quick approximation:
B = sum(T.bytes)
Star Strider
on 22 Jan 2024
@Steven Lord — Using struct2table is definitely more efficient. I don’t use struct arrays very often, so I didn’t consider using it.
More Answers (2)
Hassaan
on 19 Jan 2024
The whos command by itself displays the information in the command window, but it doesn't return that information directly to a variable. To store this information in a variable, you need to use the evalin function with 'base' as the first argument, which evaluates the whos command in the base workspace and captures its output.
% Store the information about workspace variables in a struct array
workspaceVars = evalin('base', 'whos');
% Now you can process workspaceVars as needed
% For example, to display the names and sizes of the largest variables:
for i = 1:length(workspaceVars)
fprintf('Variable Name: %s, Size: %d bytes\n', workspaceVars(i).name, workspaceVars(i).bytes);
end
% You can sort them by size if you want to focus on the largest variables
[~, sortedIndices] = sort([workspaceVars.bytes], 'descend');
sortedVars = workspaceVars(sortedIndices);
% Display the sorted information
for i = 1:length(sortedVars)
fprintf('Variable Name: %s, Size: %d bytes\n', sortedVars(i).name, sortedVars(i).bytes);
end
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Feel free to contact me.
0 Comments
See Also
Categories
Find more on Data Type Conversion 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!