Unrecognized function or variable

조회 수: 27 (최근 30일)
Sonali
Sonali 2023년 11월 3일
댓글: Sonali 2023년 11월 3일
I have a structure with n fields(bin_i). Each field has columns gender, age, mass, height and so on. I need to plot height v/s mass with error bars for mean values of each bins. I made following program:
For i =1:n
a=my_structure.(['bin_' num2str(i)])
m1=mean(a.mass(a.age==44,:))
m2=mean(a.mass(a.age==32,:))
err_1=log10(std(a.mass(a.age==44,:)))
err_2=log10(std(a.mass(a.age==44,:)))
scatter(log10(m1),mean(a.height(a.age==44,:)),'red',"filled")
hold on
scatter(log10(m2),mean(a.height(a.age==32,:),'green',"filled")
hold on
errorbar(log10(m1),mean(a.height(a.age==44,:)),err_1,'horizontal')
hold on
errorbar(log10(m2),mean(a.height(a.age==32,:),err_2,'horizontal')
hold on
end
Sometimes it creates the plots and sometimes it shows the error:Unrecognized function or variable 'm1'. I am unable to figure out the the cause, since it does run at times. But I would really appreciate some solution for this error. Thanks

답변 (1개)

Image Analyst
Image Analyst 2023년 11월 3일
Please post your code or attach it. There is no "For" in MATLAB. Plus your shown program uses my_structure and n without ever defining them. So I know that code you posted is not your actual code or you would have different errors.
It could be that there were no ages of exactly 44 for your very first structure so m1 might be either null or undefined.
Don't use i as an iterator since it's the complex constant. Use ii or k or something else.
You don't need to call hold on after every errorbar. Calling it just once after scatter is enough,.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
  댓글 수: 5
Image Analyst
Image Analyst 2023년 11월 3일
I see you're using dynamic field names all over the place. This is a very bad idea.
and
Can you rewrite the program to use normal arrays or tables instead of dynamically named fields? Then I'll look at it. And fix the obvious other errors (like jj not being defined, etc.).
Sonali
Sonali 2023년 11월 3일
Okay, I will work on it and share with you. Thanks for your your time and help.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by