Uploading .mat files that contain nested structs into a datastore
Mostra commenti meno recenti
I would like to upload .mat file into a datastore that contains nested structs. Like so...
TestData.Unit1.Parameter 1
TestData.Unit1.Parameter 2
TestData.Unit2.Parameter 1
TestData.Unit2.Parameter 2
When I try to upload this into a datastore I cannot read the content of the fields inside the nested struct. The size of the .mat files are enormous and take a really long time to load one parameter. How do I accomplish this while still using datastore or something of the like? I know we are supposed to use fileDataStore to create the custom read function but the read function I created only displays the first level of the structs.

Risposta accettata
Più risposte (1)
Matt J
il 20 Feb 2024
0 voti
This example shows how to use a fileDataStore to read partial data from a .mat file
I might modify their example and use a matfile object to read the data without actually loading the entire file.
7 Commenti
Dennan
il 20 Feb 2024
Walter Roberson
il 20 Feb 2024
Spostato: Matt J
il 20 Feb 2024
data = matfile(filename, variables{1});
That is the wrong way to use matfile. The proper way to use matfile is
matObj = matfile(filename);
data = matObj.(variables{1});
If the instances to be read are the nested Parameterx fields, then I think you would need something more elaborate than the literal example from the doc:
function [data,tasks,done] = load_variable(filename,tasks)
% Initialize tasks
if isempty(tasks)
tasks.unitList = who('-file', filename);
end
%Initialize current Unit
if ~isfield(task,'currentUnit')
tasks.currentUnit=load(filename, tasks.unitList{1}).(tasks.unitList{1});
tasks.paramList=fieldnames(tasks.currentUnit);
end
% Load a piece of data
data = tasks.currentUnit.(tasks.paramList{1});
% Remove the read-out parameter from paramList
tasks.paramList(1) = [];
if isempty(tasks.paramList) %Check if we are finished processing a Unit
tasks=rmfield(tasks,{'currentUnit','paramList'});
tasks.unitList(1)=[];
end
done =isempty(tasks.unitList); %Check if all Units in the file have been processed
if done
tasks=[];
end
end
Dennan
il 20 Feb 2024
Matt J
il 20 Feb 2024
See above.
"How do you get to the actual data within the struct?"
Based on what you show in your comment, perhaps something like this:
S = read(fds);
S.Para1
S.Para2
Matt J
il 21 Feb 2024
Based on what you show in your comment, perhaps something like this:
Yes, but the OP would like the read() operation to return this directly.
Categorie
Scopri di più su JSON Format in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!