Can I use imported from matlab workspace matrix in simulink function block?

21 views (last 30 days)
In a project I created, well, I tried to create, a function that takes value of a constant as an input and outputs a corresponding value from a table, table converted to matrix to be percise. I have no problem running it in matlab, as the algorythm is pretty obvious, I also figured out how to import the matrix from workspace. Combining the two is what I struggle to achieve. When running the code below I get several errors, telling me that "alphatocl" is undefined. Would appreciate any help. I use Matlab2023b if that's necessary.
function cl = fcn(a)
cl = alphatocl(alphatocl==a,2); %alphatocl here is a 51*2 matrix, first column of which corresponds to input a, while the second column contains the corresponding values

Answers (2)

Walter Roberson
Walter Roberson 22 minuten ago
See https://www.mathworks.com/help/simulink/ug/migrate-models-to-use-dictionary.html for instructions on linking data dictionaries to the base workspace.

Paul
Paul ongeveer 22 uur ago
Assuming the intent is to implement this function in a Matlab Function block, then see Use Data in Multiple MATLAB Function Blocks by Defining Parameter Variables for how bring alphatocl into the block as a parameter from the base workspace (or alternatives). Or make alphatocl a second input to the function and feed that input from a Constant block with alphatocl being the that block's 'Constant value' parameter.
However, it seems like there may be other problems, particuarly that it's likely cl will be a different size depending on the value of 'a'. Hence, you might need to deal with Variable Sized Signals.
  2 Comments
Ostap
Ostap ongeveer 6 uur ago
Yes, the intent is to use a Matlab Function block. I don't really get how can cl be different size other than a scalar. Can you please elaborate?
Paul
Paul ongeveer 5 uur ago
Edited: Paul 12 minuten ago
I should have looked closer at the question, because it's not clear it's doing what is wanted, or at least it's fragile.
To simplify things, let's assume that alphatocl is 4x2 instead of 51x2
rng(100);
alphatocl = [(1:4)',rand(4,1)]
alphatocl = 4×2
1.0000 0.5434 2.0000 0.2784 3.0000 0.4245 4.0000 0.8448
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
assume a value for 'a'
a = 2;
The code in the question is
cl = alphatocl(alphatocl==a,2)
cl = 0.2784
I think that returned the desired result, though I believe it's using an undocumented form of indexing with the first subscrpt being a 4x2 array. But it does work, so let's go with it.
But what happens if another element in column 1 is equal to 'a'
alphatocl(4,1) = 2
alphatocl = 4×2
1.0000 0.5434 2.0000 0.2784 3.0000 0.4245 2.0000 0.8448
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
cl = alphatocl(alphatocl==a,2)
cl = 2×1
0.2784 0.8448
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
That generates two results, whereas a = 1 would generate one result. That's how cl can be something other than a scalar. If all of column 1 of alphatocl is unique, then I guess there's not a problem, as long as the input 'a' matches one value. What if it doesn't?
cl = alphatocl(alphatocl==5,2)
cl = 0×1 empty double column vector
So you might have to handle that case as well.
What if an element in column 2 matches 'a'? That will result in an error.
alphatocl(1,2) = 2
alphatocl = 4×2
1.0000 2.0000 2.0000 0.2784 3.0000 0.4245 2.0000 0.8448
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
try
cl = alphatocl(alphatocl==a,2)
catch ME
ME.message
end
ans = 'The logical indices in position 1 contain a true value outside of the array bounds.'
I suspect what you really want is to only match the elements in column 1
cl = alphatocl(alphatocl(:,1)==a,2)
cl = 2×1
0.2784 0.8448
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
If the input 'a' will always match exactly one value in the first column, then all is well. If it can match more than one, or none, then further action will need to be considered.

Sign in to comment.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!