필터 지우기
필터 지우기

Passing a string to a function

조회 수: 1 (최근 30일)
kevin
kevin 2012년 3월 20일
I have created several structures based on my data using eval. The names of the structures and the names of the fields are stored in seperate arrays and the names can be different depending on the data. How can I pass these strings into a functin such as fieldnames? I keep gettting errors that the input arguments cannot be 'cell' or 'char'. So if the name of my struct is in name(1,1) how can i run fieldnames(name(1,1))

채택된 답변

Wayne King
Wayne King 2012년 3월 20일
namestruct = 'test';
test = struct('x',randn(100,1),'y',randn(100,1));
namez = fieldnames(eval(namestruct))
  댓글 수: 2
kevin
kevin 2012년 3월 20일
Perfect! Thanks. I have another issue now. This worked fine and I now have the names of all my structs and their fields. Now how could I plot this data? I have concatenated the struct and field name together:
for i=1:1:length(namez)
struct_names{i,1}=horzcat('test.',namez{i});
end
now I want to plot struct_names{1,1}(1:100,1). I tried using eval again but could not get it to return the data.
kevin
kevin 2012년 3월 20일
Nevermind, I figured it out.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by