How to call a structure whose name is assigned to another variable???

1 回表示 (過去 30 日間)
Sameer
Sameer 2015 年 6 月 26 日
回答済み: Steven Lord 2015 年 6 月 26 日
Hello all,
I am trying to get the data from a structure but I the problem I am facing is that I am not able to call the Structure though a variable which cointains the structure name. When I am writng the name of the structure specifically then its fine but not otherwise. Can anyone be able to help.
clear all
name='Test_4';
load(name);
Nr_Variabel=length(name.Y);
for k=1:Nr_Variabel
Tem=name.Y(1,k).Data;
Variabel_name=name.Y(1,k).Name;
plot(Tem);
xlabel(Variabel_name);
end

採用された回答

Steven Lord
Steven Lord 2015 年 6 月 26 日
I recommend that you call LOAD with an output argument.
data = load('mymatfile.mat');
Now you can access, for example, the variable named x that was stored in mymatfile.mat as:
data.x
This avoids a problem where your MAT-file contains a variable with the same name as a function [often ALPHA] -- this is known as "poofing" on the MATLAB newsgroup and can be difficult to diagnose.

その他の回答 (1 件)

Adam
Adam 2015 年 6 月 26 日
I'm not really sure I understand the question, but if you mean that a field name is assigned to a variable you can do e.g.
fieldName = 'Data';
result = someStruct.( fieldName );
to access a field using a dynamic string from a variable rather than a hard-coded name.
  1 件のコメント
Sameer
Sameer 2015 年 6 月 26 日
Thanks for the answer. But I solved the issue I was having.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by