Unrecognized function or variable

24 views (last 30 days)
Sonali
Sonali on 3 Nov 2023
Commented: Sonali on 3 Nov 2023
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

Answers (1)

Image Analyst
Image Analyst on 3 Nov 2023
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 Comments
Image Analyst
Image Analyst on 3 Nov 2023
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 on 3 Nov 2023
Okay, I will work on it and share with you. Thanks for your your time and help.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!