Inserting doubles into the field of a struct

14 ビュー (過去 30 日間)
BA
BA 2022 年 9 月 19 日
コメント済み: BA 2022 年 9 月 19 日
Wondering if anyone knows how to insert a "double" into a struct. I was trying to use the following code to do it but I keep getting the error "Scalar structure required for this assignment."
Updating.GoodNews = Upda;

採用された回答

Chunru
Chunru 2022 年 9 月 19 日
load(websave("Updating.mat", "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1129165/Updating.mat"))
load(websave("Upda.mat", "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1129170/Upda.mat"))
whos
Name Size Bytes Class Attributes Optimism_EMA_Updating 29x1 3312 struct Upda 28x31 6944 double cmdout 1x33 66 char
% Optimism_EMA_Updating is an array of structure
% assignment should be done for struct element
Optimism_EMA_Updating(1).GoodNews = Upda;
Optimism_EMA_Updating(1)
ans = struct with fields:
SubID: 21760059 GoodNews: [28×31 double]
  1 件のコメント
BA
BA 2022 年 9 月 19 日
Thank you!

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 9 月 19 日
It's because Updating is a structure array so you need to put Upda into a new structure in the array. This works, where I append Upda to the existing array:
% Load mat files into structures.
s1 = load('Updating.mat')
s2 = load("Upda.mat")
% Extract variables from the structures.
Optimism_EMA_Updating = s1.Optimism_EMA_Updating
Upda = s2.Upda
% Append Upda as a new structure at the end of the structure array
Optimism_EMA_Updating(end+1).SubID = Upda
  1 件のコメント
BA
BA 2022 年 9 月 19 日
Thank you for the help! Unfortunately, I think CHunru's answer came a few seconds before yours. I wish I could accept both but I don't think I can

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

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by