Set 2 structure fields to be the same result when 1 is a function call within a structure element

2 views (last 30 days)
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 Comments
John H.
John H. on 7 Feb 2023
Yup Yup, i can certianly do that. But i am trying to avoid the neccessity of having 2 lines of code for every one data call. If its not possible then sure ill do what i have to do. But I am really hoping that someone smarter than I knows the solution.
Think of my objective as having a command set, i call 1 command by typing the structure CallOutput = A.B.C2(X,Y)
Then sometime later, a completely different portion of the code, maybe not even the same function, i pass structure A, call A.B.C1 and get that value again without needing to push a data call to the hardware for it.
It becomes super annoying to instead need to do this every time.
CallOutput = A.B.C2(X,Y);
A.B.C1 = A.B.C2(X,Y);
There has to be a sneaky way to embedd that command into the
A.B.C2 = @(x,y) Test(x,y); code in the backend.
Maybe there is a completely better way to do this that doesnt use the structure topology, i dont know but im all ears.

Sign in to comment.

Answers (1)

Jan
Jan on 7 Feb 2023
This calls the function once only and copies the result from the cache in the next call.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!