Set 2 structure fields to be the same result when 1 is a function call within a structure element
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello all,
Kind of long winded here, but im just trying to explain myself as best i can. My need, issue, rational, etc.
I am using matlab to interface to a hardware device, I am trying to formulate the most convienient method to structure the commands list, which right now seems to be by building a strucure where i segregate commands based on which device they are a part of etc.
Ex:
A.B.C1 = @(x,y) Test(x,y)
Is a command C1, associated with hardware device A submodule B
The structure doesnt hold the communication protocol directly, but rather address/instructions for a particular port.
Here simulated by variables X and Y which are sent to the simulated communication function "Test" to completed the coms connection and get the data value.
Its important to note that there are hundreds of "Commands" which i can issue to the same communication function.
So I need to keep the communication function as general as possible.
Due to the actual communication function generating a network load, there are instances where i dont need to actually open a coms port to get a data value, and would rather just utilize a stored value from a previous call.
But... every. single. time. that i want to open a communication port to get a value I dont want to have to write a seperate line of code to assign the value back to the structure.
Ex:
CallOutput = A.B.C2(X,Y) %Induces the network load, gather the data from the hardware device
A.B.C1 = CallOutput % this the line of code is what i am trying to avoid needing to do everysingle time i need to communicate
%Do other things
value = A.B.C1 %reading the stored value from the structure at some later time
So, I would really like it. If when i perform the communication call
A.B.C2(X,Y)
It automatically assigned the output to the
A.B.C1
structrure element as well, while also allowing me to assign that value to a seperate variable.
I thought i could use the deal() function to make this work.
But it just seems to pass the function handle to A.B.C1 instead of the value. which makes sense
X = 1
Y = 2
[A.B.C1,A.B.C2] = deal(@(x,y) Test(x,y));
CallOutput = A.B.C2(X,Y)
value = A.B.C1
function [Z]=Test(X,Y)
Z=X+Y;
end
Does anyone know of a way to get a structure function to perform its task and output a value, while also assigning that value to another structure element?
I recognize this as a way to go about things, bascially use deal to make the assignment when calling that structure function
X = 1
Y = 2
A.B.C2 = @(x,y) Test(x,y);
[CallOutput,A.B.C1] = deal(A.B.C2(X,Y))
CallOutput
value = A.B.C1
function [Z]=Test(X,Y)
Z=X+Y;
end
But it just seems like there should be a way to get the assigment to occur within the actual comunication event portion of the code directly, thus eliminating addittional code that i just cant figure out.
A.B.C2 = @(x,y) Test(x,y);
Thoughts?
2 commentaires
Voss
le 7 Fév 2023
Why not just this?
A.B.C1 = A.B.C2(X,Y);
And then refer to A.B.C1 if you want to use that value for something else.
Réponses (1)
Jan
le 7 Fév 2023
You can use memoize or a simple tool like here: https://www.mathworks.com/matlabcentral/answers/1892960-memoize-save-restore-cache#answer_1148125
This calls the function once only and copies the result from the cache in the next call.
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!