Plotting signal using MATLAB function
4 views (last 30 days)
Show older comments
I have two signals, current and voltage. I want to plot Voltage signal with respect to Current sinal and finding the properties of the peak point in the plotted signal. To write the command I am using Matlab function, but it received this error:
"Function output 'y' cannot be an mxArray in this context. Consider preinitializing the output variable with a known type".
do you know how can I solve this problem?
0 Comments
Answers (1)
Walter Roberson
on 11 Aug 2020
Arrgh! my browser ate my long reply explaining what you were doing wrong!
So I am going to skip all of that and tell you to just Not Do That. Instead use https://www.mathworks.com/help/simulink/slref/xygraph.html
2 Comments
Walter Roberson
on 12 Aug 2020
You appear to be using From Spreadsheet https://www.mathworks.com/help/simulink/slref/fromspreadsheet.html
"and outputs the data as a signal"
"The From Spreadsheet block interprets the first column as time. In this column, the time values must monotonically increase."
So you have a signal being generated. You cannot just go and examine all the data points to find out where the peak is. Well, sure in theory you could attach a scope or trace on the lines and read out from the buffer of the scope or trace. but those are Not Good Ideas.
https://www.mathworks.com/help/dsp/ref/maximum.html from DSP Toolbox has a running maximum with index -- though I cannot really tell what the index is going to be for the Running case.
https://www.mathworks.com/help/simulink/slref/minmaxrunningresettable.html has running min/max, but does not appear to try to record secondary values, and has no output that goes true when a new maximum is recognized.
I therefore suggest that if you are using discrete mode, that you use a few blocks, including two memory blocks, one for the current maximum Y and one for the X associated with the current maximum Y.
If you are using continuous mode then I am not sure that max is computable.
You could also consider using a simple MATLAB Function Block with X Y inputs and a couple of persistent variables, that emits current maximum Y and associated X; might be easier than putting it together with discrete blocks.
But remember since you are processing a signal, you can only talk about "maximum so far according to the simulation time you reached, except that I seem to remember there being a way to hook in behaviour at end of simulation that could do any post processing.
I keep wanting to load entire files or arrays in Simulink, and I keep having to remind myself that No, Simulink blocks are primarily intended for signals that evolve in time.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!