multiple 1x1 cell array to struct

조회 수: 3 (최근 30일)
Desmond Harris
Desmond Harris 2023년 3월 16일
답변: Vilém Frynta 2023년 3월 16일
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에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by