whos - I can't capture the output
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
Dyuman Joshi
2024-1-19
编辑:Dyuman Joshi
2024-1-19
"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?
采纳的回答
Star Strider
2024-1-19
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 个评论
Steven Lord
2024-1-22
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
2024-1-22
@Steven Lord — Using struct2table is definitely more efficient. I don’t use struct arrays very often, so I didn’t consider using it.
更多回答(2 个)
Hassaan
2024-1-19
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!