필터 지우기
필터 지우기

How to insert fieldname character type into struct type?

조회 수: 3 (최근 30일)
Sam
Sam 2018년 11월 19일
편집: per isakson 2018년 11월 19일
Hello,
This is my code:
load('Cal_Lags.mat'); %
% there are 4 treatments. Treatment 1 consists of 30 subjects.
fn = fieldnames(Cal_Lags.Treatment1);
...
% I want to replace "Pig158320817" with data in fn
output = zIO_treatment1.Lag158320817(:,1) ...
+ mIO_treatment1.Lag158320817(1) ;
fn is a vector of 30 x 1 cell. I select the first cell with k = fn{1, 1}. Now k is character-type. I want to make a forloop to immediately calculate the output for all 30 subjects, using the 'character' in fn, the problem is I can't seemed to incorporate fn{1,1} and select the first column...
Thank you.
  댓글 수: 1
Rik
Rik 2018년 11월 19일
You mean like this?
output=0;
for n=1:numel(fn)
temp=zIO_treatment1.(fn{n});
output=output+temp(:,1);
temp=mIO_treatment1.(fn{n});
output=output+temp(1);
end

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

채택된 답변

per isakson
per isakson 2018년 11월 19일
편집: per isakson 2018년 11월 19일
A tiny experiment
>> s.f = {1,2,3,4,5,6,7};
>> name = 'f';
>> s.(name)(6)
ans =
1×1 cell array
{[6]}
>>
I'm guessing. Try something like this
len = length( fn );
output = cell( len, 1 );
for jj = 1 : len
output{jj} = zIO_treatment1.(fn{jj})(:,1) ...
+ mIO_treatment1.(fn{jj})(1) ;
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by