Hi!
I have a struct Str. the element of the structure arle A, B, C,.... all of the are arrays with length 2000. I want to have onle the element from 500:1500 without writing
Str.A1=Str.A(500:1500) for every struct element.
is there an easy way to achieve this goal?
thank you!

 採用された回答

Peter Jarosi
Peter Jarosi 2019 年 7 月 19 日
編集済み: Peter Jarosi 2019 年 7 月 19 日

1 投票

You can use for loop indexing elements of your structure:
myElements = fieldnames(Str);
% or you can list your elements
% myElements = {'A', 'B', 'C'};
for x = 1 : length(myElements)
e1 = myElements{x};
e2 = strcat(e1, '1');
Str.(e1)=Str.(e2)(500:1500);
end
Please, let me know if it works for you

2 件のコメント

Rica
Rica 2019 年 7 月 19 日
it works! but you should change this:
e2 = strcat(e1);
Thank you!
Peter Jarosi
Peter Jarosi 2019 年 7 月 19 日
編集済み: Peter Jarosi 2019 年 7 月 19 日
You're very welcome!
You are right, if you want to overwrite your existing instances. In this case strcat() is even unnecessary:
myElements = fieldnames(Str);
% or you can list your elements
% myElements = {'A', 'B', 'C'};
for x = 1 : length(myElements)
e1 = myElements{x};
Str.(e1)=Str.(e1)(500:1500);
end
but in your question you wrote:
Str.A1=Str.A(500:1500);
that's why I thought that you want to create new elements of your structure in order to save old ones.

サインインしてコメントする。

その他の回答 (2 件)

Mario Chiappelli
Mario Chiappelli 2019 年 7 月 19 日
編集済み: Mario Chiappelli 2019 年 7 月 19 日

1 投票

I would loop through each A,B,C... Other languages call this a for each loop.
for str = {'A','B','C'}
variableName = strcat(str{1},'1');
Str.(variableName) = Str.(str{1})(500:1500);
end
Note: Instead of looping through numbers where each iteration is stored in the for statement variable "i" (or whatever you decide to call it), you are looping through each letter and the for statement variable in this case is "str".

6 件のコメント

Peter Jarosi
Peter Jarosi 2019 年 7 月 19 日
編集済み: Peter Jarosi 2019 年 7 月 19 日
I think your code won't work, because, for instance you should use Str.(variableName) syntax instead of Str.variableName (and other problems).
Mario Chiappelli
Mario Chiappelli 2019 年 7 月 19 日
What are these "other problems", you did exactly what I did but you put the parantheses around the variables.
Peter Jarosi
Peter Jarosi 2019 年 7 月 19 日
編集済み: Peter Jarosi 2019 年 7 月 19 日
I didn't see your code when I was writing mine. You were just faster than me in order to submit it, and less accurate, and still having syntax errors. Test it first, before submit!
Peter Jarosi
Peter Jarosi 2019 年 7 月 19 日
It's a nice trick:
for str = {'A','B','C'}
but in this case doesn't work properly, because you will get 1x1 cells instead of strings. So you need one more step in order to convert them to strings, otherwise you won't be able to use them inside the parenthesis syntax.
Peter Jarosi
Peter Jarosi 2019 年 7 月 19 日
Good job! I've given you a vote for it.
One little thing is left. Change "1" to '1'
Mario Chiappelli
Mario Chiappelli 2019 年 7 月 19 日
*thumbs up emoji

サインインしてコメントする。

Rica
Rica 2019 年 7 月 19 日

0 投票

Thank you to every one!

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by