define the local variable in Function to global

function data= ReadGrasshopperFile( ~, ~,~)
x = randi(10, 1);
y = randi(10, 1);
z = randi(10, 1);
parameters = [x, y, z];
fileName1='M2GHfileName.csv';
fileName2='GH2Matlab.csv';
writematrix(parameters, fileName1);
data = readmatrix(fileName2);
end
This function that I defined it works good.
But, I wanted the x and y and z changed each time that I recall function.
what should I do?

댓글 수: 10

At the moment you are writing one file but reading a different file.
You could create random data, or you could use random numbers to modify the file you read, or if you have several files to read from you could read one at random.
@Walter Roberson Thanks for answering but I dont understand what should I do, I have a basic knowledge in matlab.
At the end of the function before the end statement add
data = data + randi(10, size(data)) ;
It doest not work as some thing I wanted. I wanted when I run file it gives me a new number and results. because I want to call function in optimization toolbox.
Torsten
Torsten 2022년 2월 19일
편집: Torsten 2022년 2월 19일
The optimization toolbox can't cope with random inputs changing with iteration.
Use stochastic optimization tools instead.
Jan
Jan 2022년 2월 19일
"I wanted when I run file it gives me a new number and results." - but this does happen already: randi replies random integers. As long as you do not reset the random number generator manually before, you get new values.
Yeah, I won't use stochastic optimization tools; But my problem is that when I use this function as an object to be optimized, it doesn't create a random number. In addition, if I don't use the function after each run, it works great like the below code.
x = randi(10, 1);
y = randi(10, 1);
z = randi(10, 1);
parameters = [x, y, z];
fileName1='M2GHfileName.csv';
fileName2='GH2Matlab.csv';
writematrix(parameters, fileName1);
data = readmatrix(fileName2);
Thank you so much, this is my answer "data = data + randi(10, size(data)) ;". I really appreciate.
I don't understand what you are trying to do, but I repeat:
The optimization toolbox cannot cope with random inputs that change with iteration.
So you can create random input, but it must remain constant during the optimization process.
I wanted to creat a function that gives ne random number and save these variable in csv file, after a sleep time read other csv file. I dont want to use optimization toolbox; I use a swarm optimization.

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2022년 2월 19일

댓글:

2022년 2월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by