Can I use imported from matlab workspace matrix in simulink function block?
21 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
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.
More about global data stores is at https://www.mathworks.com/help/simulink/ug/model-global-data-using-data-stores.html
0 Comments
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
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)]
assume a value for 'a'
a = 2;
The code in the question is
cl = alphatocl(alphatocl==a,2)
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
cl = alphatocl(alphatocl==a,2)
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)
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
try
cl = alphatocl(alphatocl==a,2)
catch ME
ME.message
end
I suspect what you really want is to only match the elements in column 1
cl = alphatocl(alphatocl(:,1)==a,2)
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.
See Also
Categories
Find more on Simulink Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!