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