フィルターのクリア

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

1 回表示 (過去 30 日間)
Wiktoria Glogowska
Wiktoria Glogowska 2019 年 7 月 30 日
コメント済み: Wiktoria Glogowska 2019 年 7 月 30 日
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 件のコメント
Stephen23
Stephen23 2019 年 7 月 30 日
編集済み: Stephen23 2019 年 7 月 30 日
Do not use eval for trivial code, like accessing fields of a structure. Read this to know why:

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2019 年 7 月 30 日
編集済み: Stephen23 2019 年 7 月 30 日
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 件のコメント
Wiktoria Glogowska
Wiktoria Glogowska 2019 年 7 月 30 日
Thank you. The code for one variable worked.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by