Effacer les filtres
Effacer les filtres

How can.struct data be made available to non-MATLAB users?

48 vues (au cours des 30 derniers jours)
Oswin Hulsebos
Oswin Hulsebos le 1 Juil 2024 à 10:59
Commenté : Oswin Hulsebos le 1 Juil 2024 à 15:26
I have several scripts which are based on a structure containing multiple fields of data, with nested structures inside. This structure is as is, as it is generated automatically and is used for further processing. However, the dependencies and structure of the work indicated inside this struct are also required for people who do not use MATLAB.
Hence, I'm looking for one of two solutions, which I was unable to find so far.
  1. Can a .struct with nested structs, using a multitude of fieldnames and datalines, be converted to a non-MATLAB format like excel? The command "writetable(struct2table(structname, 'filename.xlsx')" works, but does not include the nested data which can be multiple layers deep.
  2. Can the .struct data be saved such that it can be viewed by someone without MATLAB? (e.g. does MATLAB provide something like a 'viewer'?)
  2 commentaires
Steven Lord
Steven Lord le 1 Juil 2024 à 14:30
Can the .struct data be saved such that it can be viewed by someone without MATLAB? (e.g. does MATLAB provide something like a 'viewer'?)
It's not exactly what you're asking for, but if you have people who only need to view the data occasionally (for up to 20 hours per month) they could use the basic version of MATLAB Online to view the data.
Oswin Hulsebos
Oswin Hulsebos le 1 Juil 2024 à 15:26
It is actually a very viable answer to my question, I likely just phrased it too broad to be completely correct.
Likely, this may even be preferred as I suppose MATLAB online does not require any installation!

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 1 Juil 2024 à 11:10
Modifié(e) : Stephen23 le 1 Juil 2024 à 11:24
"Can a .struct with nested structs, using a multitude of fieldnames and datalines, be converted to a non-MATLAB format like excel?"
In general: no. A table is fundamentally 2D numeric/text data. Nested structures have arbitrary depth and content.
File formats such as JSON are limited to that which can be reasonably represented in text, but does support nested arrays. JSON (or similar) might be a suitable option for your "people":
"Can the .struct data be saved such that it can be viewed by someone without MATLAB? (e.g. does MATLAB provide something like a 'viewer'?)"
Rather than mangling the data just SAVE it as a MAT file, your "people" can open it using other (free) tools, e.g.:
  1 commentaire
Oswin Hulsebos
Oswin Hulsebos le 1 Juil 2024 à 11:32
Thank you for your reply! The main essence of getting the information across appears possible with the other comment, using .JSON as you also suggest. I'll accept your answer as it covers both subquestions.

Connectez-vous pour commenter.

Plus de réponses (1)

Abhishek Kumar Singh
Abhishek Kumar Singh le 1 Juil 2024 à 11:08
You can save the structure as a JSON file.
JSON is a widely used format that can be easily viewed and manipulated outside of MATLAB.
Refer to the documentation page of jsonencode to know more about creating JSON-formatted text from structured MATLAB data: https://www.mathworks.com/help/matlab/ref/jsonencode.html
Also, here's an example snippet you can refer to:
% Example nested structure
exampleStruct.a = 1;
exampleStruct.b.b1 = 2;
exampleStruct.b.b2.b21 = 3;
exampleStruct.b.b2.b22 = 4;
exampleStruct.c = 5;
disp(exampleStruct)
a: 1 b: [1x1 struct] c: 5
% Write to JSON
jsonString = jsonencode(exampleStruct);
fid = fopen('nestedStruct.json', 'w');
fwrite(fid, jsonString, 'char');
fclose(fid);
Hope it helps!
  1 commentaire
Oswin Hulsebos
Oswin Hulsebos le 1 Juil 2024 à 11:28
Although not a format I typically use due to the less graphical representation, this seems to work perfectly. From what I could see all nested data is loaded correctly too! Thank you!

Connectez-vous pour commenter.

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by