Saving workspace variables into one struct

I have a workspace containing
date (178x1 cell)
opposing_team (178x1 double)
team (178x1 double)
win (178x1 double)
I want to merge these variables into 1 struct. I tried using this code here
data = struct('date',{''},'opposing_team',[],'team',[],'win',[]);
but it deletes the data inside it, so when I open the variables it returns with a blank table. How do I create a struct that will keep the data inside? Thanks in advance.

4 件のコメント

Stephen23
Stephen23 2017 年 11 月 28 日
@Adhi Ariawan: do you want a scalar structure containing arrays, or a non-scalar structure containing scalars?
Adhi Ariawan
Adhi Ariawan 2017 年 11 月 28 日
編集済み: Adhi Ariawan 2017 年 11 月 28 日
I'm not sure what scalar and non-scalar is.. but date is a dd/mm/yyyy format; opposing_team, team and win are integers. They are single columns containing rows of data. I want to save these data as is inside a struct.
Birdman
Birdman 2017 年 11 月 28 日
Check my answer.
Stephen23
Stephen23 2017 年 11 月 28 日
@Adhi Ariawan: "scalar" means that the array has size 1x1:

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

 採用された回答

Stephen23
Stephen23 2017 年 11 月 28 日

0 投票

Here are the two main options. First we will use this fake data:
>> date = num2cell(randi(365,178,1));
>> oppt = rand(178,1);
>> team = rand(178,1);
>> win = rand(178,1);
Non-Scalar structure of with scalar fields:
>> S = struct('date',date,'oppteam',num2cell(oppt),'team',num2cell(team),'win',num2cell(win));
>> size(S)
ans =
178 1
>> S(1)
ans =
scalar structure containing the fields:
date = 25
oppteam = 0.17320
team = 0.82512
win = 0.46835
Scalar structure with non-scalar fields:
>> S = struct('date',{date},'oppteam',oppt,'team',team,'win',win);
>> size(S)
ans =
1 1
>> S.date{1}
ans = 25
>> S.oppteam(1)
ans = 0.17320
>> S.team(1)
ans = 0.82512
>> S.win(1)
ans = 0.46835

4 件のコメント

Adhi Ariawan
Adhi Ariawan 2017 年 11 月 28 日
Scalar structure with non-scalar fields was what I was looking for. Thanks Stephen.
Samy Alkhayat
Samy Alkhayat 2022 年 8 月 8 日
@Stephen23 Hello Stephen
I have so many non-scalar structures in my workspace and I want to use the command above without having to writing name by name, can you please advise?
Thanks in advance.
Stephen23
Stephen23 2022 年 8 月 8 日
編集済み: Stephen23 2022 年 8 月 9 日
"I have so many non-scalar structures in my workspace and I want to use the command above without having to writing name by name, can you please advise"
Depending on what their names are and how many other variables there are, you could possibly use SAVE with its -regexp option or something similar. Read the SAVE documentation.
Samy Alkhayat
Samy Alkhayat 2022 年 8 月 8 日
I have figured it out, that was helpful, thank you!

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2017 年 11 月 28 日

編集済み:

2022 年 8 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by