Display Parameter with Properties in Command Window

조회 수: 4 (최근 30일)
Nick
Nick 2016년 12월 1일
댓글: Nick 2016년 12월 6일
Hello,
I would like to loop through the variables in my Workspace and display the properties of certain variables in the Command Window. Using "whos", I can loop through all the variables and filter them as I desire:
tempvar_s = whos;
for tempvar_i = 1:length(tempvar_s)
% Include any filters here in the IF statement clauses
if (strcmp(tempvar_s(tempvar_i).name, 'tempvar_s')) || (strcmp(tempvar_s(tempvar_i).name, 'tempvar_i')) || (strcmp(tempvar_s(tempvar_i).name, 'tempstrFileName'))
% do nothing
else
disp(tempvar_s(tempvar_i).name)
disp(tempvar_s(tempvar_i))
end
end
My problem is that when I use the "disp" command to display the variable using my script, I get the structure from whos, like in the following example:
p1_kPa
name: 'p1_kPa'
size: [1 1]
bytes: 112
class: 'Simulink.Parameter'
global: 0
sparse: 0
complex: 0
nesting: [1x1 struct]
persistent: 0
But if I type the same example variable into the Command Window and hit enter, I get a different list:
p1_kPa =
Parameter with properties:
Value: 101.3250
CoderInfo: [1x1 Simulink.CoderInfo]
Description: 'Ambient pressure in kPa'
DataType: 'single'
Min: 0
Max: 1000
DocUnits: 'kPa'
Complexity: 'real'
Dimensions: [1 1]
Can anyone advise how I can get my script to print out the second data set rather than the first?

답변 (1개)

Vishal Neelagiri
Vishal Neelagiri 2016년 12월 6일
This is an expected behavior because when you use 'tempvar_s = whos' command in your script, you are saving specific information about the workspace variables into a structure array named 'tempvar_s'. This struct has the fields like name, size, bytes etc.
In order to display the dataset similar to the second one rather than the first, you can try using a script similar to this:
a = 4;
p1 = Simulink.Parameter;
save test.mat a p1
x = load('test.mat')
x.p1
  댓글 수: 1
Nick
Nick 2016년 12월 6일
Vishal, thank you for the reply. Unfortunately this still doesn't help me as I can't loop through all the variables with this approach, correct? I have to manually type each variable name.

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

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by