how to avoid using a name to a specific file in GUI

1 vue (au cours des 30 derniers jours)
Wiktoria Glogowska
Wiktoria Glogowska le 30 Juil 2019
Hello,
We are creating a GUI in Matlab guide. The problem I have is that I am accesing the data from a structure within structure which has fields such as data and time (which I need). The code is written in such a way that the name of the paritucular loaded field is always in the path, how to avoid this?
Here is how I am accesing the data:
[filename,pathname]=uigetfile('*.mat') %,'Select One or More Files','MultiSelect', 'on')
MyData= load(filename);
handles.MyData = MyData;
MyData=handles.MyData;
But then I am eding up with this structure within structure with a code that only works for a specific filename:
time=MyData.Specificname.fieldA(1,1)
This is what I have tried without success:
% fn=char(fieldnames(MyData))
% s=sprintf('h.SD=MyData.%s',fn);
% eval(s);
% h.SD=fn
Thanks in advance!
  1 commentaire
Stephen23
Stephen23 le 30 Juil 2019
Modifié(e) : Stephen23 le 30 Juil 2019
Do not use eval for trivial code, like accessing fields of a structure. Read this to know why:

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 30 Juil 2019
Modifié(e) : Stephen23 le 30 Juil 2019
Your explanation is not clear if you know Specificname in advance or not, or how many variables there are saved in the .mat file. None of the names that you use in your example code match the names that you use in your explanation. Remember that we can only read what you write here.
If there is only one variable in the .mat file then you can do this:
[F,P]=uigetfile('*.mat')
S = load(fullfile(P,F));
C = struct2cell(S);
T = C{1};
T.data
T.time
If there are multiple fields, then you could use dynamic fieldnames, e.g.
S = load(fullfile(P,F));
T = S.('Specificname');
T.data
T.time
Note that if Specificname is different in each .mat file then the data has not been designed very well. It is much easier to parse multiple mat files when then variable names are the same in each file.
  1 commentaire
Wiktoria Glogowska
Wiktoria Glogowska le 30 Juil 2019
Thank you. The code for one variable worked.

Connectez-vous pour commenter.

Plus de réponses (0)

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!

Translated by