How to store multiple outputs of a structure into a character array using logical indexing.

2 ビュー (過去 30 日間)
Hello all,
Im new to Matlab and using structure, char and string arrays. Was hoping for some help on the below.
If I have a structure with different names as below, and if I have a logical array to weed out names containing FLUID, my logical array looks like log=[1;0;1;1;0]
Using the command F(log).name in the command line Yields the below output.showing all 3 names. However, if Id like to store all 3 values into a strucuture, or character/string array. Using X=F(log).name, just assings the first value 'Fluid 1'.
How could I dynamically assign all 3 values to another structure or string array?
Command Line
ans =
'Fluid1'
ans =
'Fluid2'
ans =
'Fluid3'
F(1).name='Fluid1' F(2).name='Solid1' F(3).name='Fluid2' F(4).name='Fluid3' F(5).name='Solid2'

採用された回答

Stephen23
Stephen23 2023 年 6 月 9 日
編集済み: Stephen23 2023 年 6 月 9 日
F(1).name='Fluid1';
F(2).name='Solid1';
F(3).name='Fluid2';
F(4).name='Fluid3';
F(5).name='Solid2';
L = logical([1;0;1;1;0]); % do NOT use LOG as a variable name
To a cell array, and then to a string array:
C = {F(L).name}
C = 1×3 cell array
{'Fluid1'} {'Fluid2'} {'Fluid3'}
S = string(C)
S = 1×3 string array
"Fluid1" "Fluid2" "Fluid3"
To another structure, with a different fieldname:
D = struct('X',cell(1,3));
[D.X] = F(L).name
D = 1×3 struct array with fields:
X
D.X % check the content
ans = 'Fluid1'
ans = 'Fluid2'
ans = 'Fluid3'

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by