Replace certain columns in a structure array

Hi. I have a structure array, Data, of size (1 x120). The 5th column of the field Data.Temp contains zeroes. I want to replace these zeroes with a number 20 from Data(30).Temp to Data(60).Temp. Also, I want to replace these zeroes from Data(61).Temp to Data(80).Temp with a number 24. I am unable to perform this operation. I kindly request some help from the community.

 採用された回答

Walter Roberson
Walter Roberson 2024 年 5 月 15 日

0 投票

[Data(30:60).Temperature] = deal(20);
[Data(61:80).Temperature] = deal(24);

7 件のコメント

Tomer
Tomer 2024 年 5 月 15 日
編集済み: Tomer 2024 年 5 月 15 日
Hi, thanks for your reply. It replaces the entire field with the numbers 20 and 24. Also, it truncates the file to size 1x80. I am attaching a PDF snapshot of how the structure looks. This is my first seeing such structure and not sure about the terminology for it. As you can see the 5th column contains zeroes. I want to replace these zeroes with some numbers.
Stephen23
Stephen23 2024 年 5 月 15 日
@Tomer: we cannot run code on a PDF of screenshots of your data.
Please upload your data in a MAT file, by clicking the paperclip button.
Tomer
Tomer 2024 年 5 月 15 日
Thanks, uploaded.
Walter Roberson
Walter Roberson 2024 年 5 月 15 日
Oh, wait, I missed the part about the 5th column.
Unfortunately, there is no way to set particular columns using deal or equivalent. You will need to
for K = 30:60; Data(K).Temperature(5) = 20; end
for K = 61:80; Data(K).Temperature(5) = 24; end
Tomer
Tomer 2024 年 5 月 15 日
Hi @Walter Roberson. Many thanks for your reply. I just ran the code on the MAT file that I attached in the last reply. Unfortunately, it does not replace the zeroes with the desired values.
Walter Roberson
Walter Roberson 2024 年 5 月 15 日
You renamed the field since the original posting.
Also it turns out that the fields contain arrays of data.
for K = 30:60; Data(K).Temp(:,5) = 20; end
for K = 61:80; Data(K).Temp(:,5) = 24; end
Tomer
Tomer 2024 年 5 月 15 日
@Walter Roberson: Thank you. It is working now.

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2024 年 5 月 15 日

コメント済み:

2024 年 5 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by