Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

How to store a field of 1x5 structure in a variable(array) in MATLAB?

I want to store all values of a field of a 1x5 structure in a variable(array) in MATLAB... See the attached screenshots for better understanding.

Script

Workspace Variables

Structure Data/Content


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Dot-Indexing by Field Name and Formatting as an Array

To access the specific field dot indexing by the structure, I by the field name in this case Overshoot can be done. To format this as an array the square brackets [] can be used to convert the cell array to a row vector. To convert this to a column-vector like how it appears in structure, I we can transpose this by using the .' term.

Overshoot = [I.Overshoot].';

Accessing Structure Field

Test Script:

%Random structure to test%
I(5).RiseTime = [];
I(5).SettlingTime = [];
I(5).SettlingMin = [];
I(5).SettlingMax = [];
I(5).Overshoot = [];
I(5).Undershoot = [];
I(5).Peak = [];
I(5).PeakTime = [];

%Filling the random structure%
for Index = 1: 5
    I(Index).RiseTime = rand(1,1);
    I(Index).SettlingTime = rand(1,1);
    I(Index).SettlingMin = rand(1,1);
    I(Index).SettlingMax = rand(1,1);
    I(Index).Overshoot = rand(1,1);
    I(Index).Undershoot = rand(1,1);
    I(Index).Peak = rand(1,1);
    I(Index).PeakTime = rand(1,1);
end

Overshoot = [I.Overshoot].';

Ran using MATLAB R2019b


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...