multiple 1x1 cell array to struct

4 次查看(过去 30 天)
I am importing a text file into matlab, converting the text to a table and importing each row with numbers as a cell array. I would like to convert each line into individual numbers then insert into a struct. Is there a good approach to this? Example below
line 87: '0.200 0.486 0.000 0.486 0.001 0.100 9.670 -40.808 10.017 3.190 7.886'
=> convert each text number (to an actual number)....
x(1,1) = 0.200,
x(1,2) = 0.486, etc... all while importing into a struct

回答(1 个)

Vilém Frynta
Vilém Frynta 2023-3-16
After your loop ends, you can simply assign all the numbers into a column of a struct. It would be much faster than importing in a loop.
I hope this is what you needed. Hope it helped.
% Vector from 1 to 100
x = 1:1:100
x = 1×100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
% Import into struct (as a 1 column)
struct.Nums = x
struct = struct with fields:
Nums: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 … ]

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by