Greetings all,
I have a question regarding the Structure field value. I'm trying to change a field of my structure that is already in the workspace. as follows: mys(1).id = {100};
I can do that from the command line and the value changes, but that is not happening from inside the function. Any idea why is that ?

댓글 수: 2

KSSV
KSSV 2016년 11월 9일
Are you sending the data which in work-space to the function? Show us your code.
MoHAJNEN komo olkm
MoHAJNEN komo olkm 2016년 11월 9일
편집: MoHAJNEN komo olkm 2016년 11월 9일
I have two functions: 1-function [graphVector]=grid2graph(xGrid,yGrid,flag) graphVector is structure. 2-function [path]=graph_search(graphVector,idxStart,idxGoal) This function does something, but I need to change some fields from the output of the first function.
That is what I'm trying to do. So, the final update should be something like this: graphVector = setfield(graphVector, {1}, 'node_index', {200}); (But again, this is from the second function)

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

 채택된 답변

Walter Roberson
Walter Roberson 2016년 11월 9일

0 개 추천

You need to have the entire variable as output from your function. Or if the function returns just the new value then the caller needs to assign it to the field by name.

댓글 수: 3

The ways to change a value on the outside of a function are:
1) make the new value an output of the function and have the caller assign the output to a variable
2) use assignin() or evalin() neither of which are recommended.
Thank you for your reply, can you please provide me with an example of the first method.
function s=test(s)
s.something = newvalue;
Called with
A = test(A)

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

추가 답변 (1개)

Guillaume
Guillaume 2016년 11월 9일

0 개 추천

Matlab is pass-by-value. functions always receive the copy of the variable. Any change you make to variables are only local to the function unless the function also returns the variable as an output and the caller assigns that output back to the original variable.

카테고리

제품

Community Treasure Hunt

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

Start Hunting!

Translated by