How to export struct saved in workspace to text file
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
ProgramNerd
le 1 Août 2022
Commenté : ProgramNerd
le 1 Août 2022
Hi, I have a struct with some fields inside. I want to be able to export the struct (or at least 2 of the fields inside) to a text file. Any help would be appreciated. Thanks
6 commentaires
Walter Roberson
le 1 Août 2022
see https://www.mathworks.com/help/matlab/ref/jsonencode.html#mw_69b38ba2-26d1-4025-a426-d1bf8ca939a7
Réponse acceptée
Abderrahim. B
le 1 Août 2022
Modifié(e) : Abderrahim. B
le 1 Août 2022
Convert to table and then wrtie to text file:
% Example 1: convert and write to table
S.Str = 'I Love MATLAB'
S.Date = datetime("now")
Stb = struct2table(S)
writetable(Stb, "Stb.txt")
Edit:
- based @Walter Roberson reply, jsonencode is a good option in your case.
% Example 2: using jsonencode and fprintf
clear
S.A1 = [9.9, 9900];
S.A2 = [8.8, 7.7 ; ...
8800, 7700];
S = jsonencode(S, "PrettyPrint", true)
- Export to text file
I do not know about your struct, so I'm skipping formatSpec.
fileID = fopen('myS.txt','w');
nbytes = fprintf(fileID,S) ;
fclose(fileID);
Hope this helps
5 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!