Unrecognized function or variable
조회 수: 27 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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
2023년 11월 3일
I see you're using dynamic field names all over the place. This is a very bad idea.
See the FAQ: https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1,_A2,...,_A10_in_a_loop?
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.).
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!