How to apply a function to an specific column of all variables?

조회 수: 4 (최근 30일)
Larissa Perez
Larissa Perez 2016년 11월 17일
댓글: Larissa Perez 2016년 11월 18일
Hi everyone! I'm new to Matlab, so I'm sure this is a silly question.
I have about 20 variables, all same size matrix. I would like to turn all the values above 50 or below -50 in the first column into NaN, for all the variables. Can someone help me with that? This is what I have been trying to do:
myvars = who;
for i = 1:length(myvars)
data = eval(myvars{i});
shear = data(:,1);
shear(shear>50)=NaN;
shear(shear<-50)=NaN;
data(:,1) = shear;
eval(myvars{1}) = data;
end
  댓글 수: 1
KSSV
KSSV 2016년 11월 17일
You have variables saved in workspace which are matrices and in there in first column you want nan? What problem you are facing now with the above code?

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

채택된 답변

James Tursa
James Tursa 2016년 11월 17일
편집: James Tursa 2016년 11월 17일
Try this:
eval([myvars{i} ' = data']);

추가 답변 (1개)

Jan
Jan 2016년 11월 17일
Don't do this. It is a bad programming style, because it impedes the debugging and performance. You find many discussions in this forum concerning eval, e.g. http://www.mathworks.com/matlabcentral/answers/194902-how-can-use-a-for-loop-to-name-a-matrix#answer_172980.
  댓글 수: 2
Larissa Perez
Larissa Perez 2016년 11월 18일
What would be the appropriate way to do that then?
Larissa Perez
Larissa Perez 2016년 11월 18일
I have tried reorganizing my data in 6 structures that I called m1-m6, and tried the following code. Still didn't work, don't know what's wrong :(
location = {'m1','m2','m3','m4','m5','m6'};
for i = 1:6
fields1=fieldnames(m1);
fields2=fieldnames(m2);
fields3=fieldnames(m3);
fields4=fieldnames(m4);
fields5=fieldnames(m5);
fields6=fieldnames(m6);
fields = {'fields1','fields2','fields3','fields4','fields5','fields6'};
loop = getfield (location{i},{fields{i}});
end
It doesn't seem to like using fields{i}

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by