Exporting Structures Containing syms
1 次查看(过去 30 天)
显示 更早的评论
How do I export the following fields with its corresponding values to a text or Excel file? I don't want to export each field manually since I am going to deal with larger structures later.
Note: all the values are numbers, but some of them are negative.
Thanks for helping!
0 个评论
采纳的回答
Walter Roberson
2021-9-9
编辑:Walter Roberson
2021-9-9
sd = structfun(@double, s, 'uniform', 0);
Now you have a numeric struct with the same field names, and can use whatever export method is suitable for that.
We do not know what you output file needs to look like, so it is difficult for us to recommend techniques.
And as mentioned above, putting each field in a cell array or converting each field individually will not work for larger structures.
Let us do some size calculations:
For a cell array, each entry is 104 bytes plus the amount of memory needed to store the data. For scalar double that is 8 bytes. So for a struct with N fields each with a scalar double, converted to cell, the total would be 112*N bytes for the cell array. You would probably also need a cell containing the fieldnames, which would be 108*N + (total number of characters in field names)*2 bytes
For a scalar struct array, each entry is 168 bytes plus the amount of memory needed to store the data. For scalar double that is 8 bytes. So for a struct with N fields each with a scalar double, the total would be 176*N bytes. This includes storage of the field names. Indeed, it works out exactly the same as 102 bytes plus 64 bytes for a field name (field names are maximum 64 characters but characters in field names are never permitted to have codes > 255, so they can be represented as one byte per character.)
So, if I understand your concern correctly, you can afford the memory for the struct, at 176*N bytes, but not the storage for the cell at 112*N + 108*N + (total number of characters in field names)*2 bytes?
Or is the concern that you cannot afford to have both the struct and the cell version in memory at the same time, that the struct uses more than half of all allocatable memory on your system? If so then that's something important to know, as it would constrain the techniques for writing to a file.
0 个评论
更多回答(1 个)
Erik Huuki
2021-9-9
编辑:Erik Huuki
2021-9-9
Once you have a workspace you can save it as a mat file for later. Simply right click in the workspace and there should be an option for saving the workspace. Or you could put them all in a cell array as shown below and write to an xlsx file.
syms a b c
example = [string(a),string(b),string(c)];
writematrix(example,'example.xlsx')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!