How to define the data type of each column in a struct?
37 次查看(过去 30 天)
显示 更早的评论
Hello,
How can i define the data type of each column in my struct ?
Size of my struct -> 1*54 struct
Now all data typs (of each column) are strings....
I tried many things without success.
splitx% is my array
Now i want to store this data (splitx(f) - string array) to my struct (parameter.sce) with formating / specific data types
splitx(f)=str2double(splitx(f))% i tried
splitx{f} = convertStringsToChars(splitx(f));%i tried
%splitx -> is my array
for zz = 1 : length(namess)% v,I,txt
parameter.sce(counteru).(namess{zz})=splitx(1,zz)%
end
My current output:
This output i want:
I need this datatype (red circles) but how / where i can define it?
Thank you in advance. Best wishes from a bloody beginner :/
0 个评论
采纳的回答
dpb
2020-7-13
编辑:dpb
2020-7-14
MATLAB is amorphous on type -- everything is stored as the type of the RHS on assignment unless directly addressing the element of an existing array of a particular type.
There's no concept of defining the type of the field of a struct; it takes on the type of the object stored in it on assignment.
So, you'll have to convert the data to the proper type before the assignment -- where to do that is probably at the point you read in the content of the splitx array from file if that's how it's input or with additional code besides just split which returns string array if that's where it originates.
We can't answer the specifics of the case not knowing those details...need code and example data (and as text, not images; can't do anything with images)
To illustrate, consider the following code snippet--
>> clear s
>> s.A='Fred';
>> s(2).A=2;
creates a struct array s whose field A contains data of different types. To prove it really works that way..
>> arrayfun(@(s) disp(s.A),s)
Fred
2.00
>>
So, the field A in the struct array s has a different data type in the two elements of the struct array. There's no knowledge on storage that the existing array fields have a particular type associated with the field name in another element of the array, nor is there any way to define a type for the field.
Continuing on the above example...
>> s(1).A=pi; % now redefine .A in s(1)
>> arrayfun(@(s) disp(s.A),s)
3.14
2.00
>>
and the conversion to double occurred silently...MATLAB simply stores what it's asked to store.
OTOH, consider
>> clear s; s=rand(3,1); % now create an array of double instead
>> whos s
Name Size Bytes Class Attributes
s 3x1 24 double
>> s(2)='Fred';
Unable to perform assignment because the left and right sides have a different number of elements.
>> s(2)={'Fred'}
Conversion to double from cell is not possible.
>> s(2)="Fred";
>>
OK, the first two acted like we would expect -- a char() string is an array so can't put more than one element of a 4x1 char array into one location. The second seems self-explanatory.
What the heck happened with the last??? No error but string "Fred" certainly is NOT numeric--so what gives?
>> whos s
Name Size Bytes Class Attributes
s 3x1 24 double
>> s
s =
0.87
NaN
0.70
>>
The array is still an array of doubles but the string class has extra features -- it tries to convert to the target numeric class and if can't just says it's "Not a Number". Internally, it's as if wrote
s(2)=str2double("Fred");
from the result.
>> s(2)="3.14"
s =
0.87
3.14
0.70
>>
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!