How to create Plot button for Complex vector?

7 views (last 30 days)
I would like to be able to highlight a complex vector in the workspace and be able to click a button to plot complex vector the way the following function works:
PTRIn = @(x) plot(1:length(x), real(x), '-b', 1:length(x), imag(x),'--g');
If I highlight a variable in the Workspace, and hit the plot button, I do not get the desired plot. (Looks like imag vs real). If I hit the stem button, I get msg: "Warning: Using only the real component of complex data."
I have releases R2020a, R2021a, R2021b, and R2022a.
  6 Comments
Paul Hoffrichter
Paul Hoffrichter on 13 Jun 2022
Edited: Paul Hoffrichter on 13 Jun 2022
>> I just don't want to be editing my code all the time by adding this function (or even adding global functions) or eliminates the need for me to add the PTRIn(x) function to each function.
To clarify... Say I have 100 functions. None of them have the PTRIn function defined in them. If examining, say, 6 of those functions, then in order to plot the complex valued vectors, I have to add PTRIn definition temporarily in those 6 functions and remove them later. Or, I can define it in the command window. I'd rather have a way where PTRIn is always defined so I don't have to keep defining it on an ad hoc basis. Would have been nice to have a PLOT button that has PTRIn defined.
>> Where should this button appear?
Anything that would help with my stated goal is better than what I do now. So, anywhere I guess. Would be nice to have a PLOT button that plots both the real and imag parts in one figure. Barring that it would be nice to a button that defines PTRIn in the command window without my having to copy and paste from my notepad.

Sign in to comment.

Accepted Answer

Jan
Jan on 13 Jun 2022
As far as I understand, all you need is to create a folder on your local disk and add it to Matlab's PATH:
addpath('D:\MyMFiles', '-end'); % Or use the PATHTOOL
Then store this file there:
function PTRIn(x)
plot(1:length(x), real(x), '-b', 1:length(x), imag(x),'--g');
end
Now you can type PTRIn(x) in the command window and in all other scripts and functions to call your function.
The functionname "PTRIn" sounds strange, but if you can remember it, everything is fine.
  13 Comments
Paul Hoffrichter
Paul Hoffrichter on 15 Jul 2022
Edited: Paul Hoffrichter on 15 Jul 2022
I am open to better approaches. Yesterday I was pleasantly surprised while stepping through the debugger. I came across something like:
Y(start + offset:stop - k)
(Y is complex vector.)
I highlighted that expression while in the debugger and typed p in the command window and got my plot. Works better than I expected.
I may try highlighting expressions like H*x + v and see if that works.
I'm not sure why this approach is bad or error prone and considered an anti pattern. Maybe if I implemented a positive pattern with your assistance, I will understand the issues better.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 15 Jul 2022
I am fairly certain that it is not possible for users to register their own plotting functions for inclusion in the Plots Catalog on the Plots tab of the toolstrip.
If you have defined PTRIn as its own function then you can call it in the Command Window using any data that is in scope (so if you're stopped in the debugger, any data accessible to the function that you're debugging is fair game. I'm being intentionally vague or general here when I say "accessible".) If you have a hard and fast requirement that this be accessible via a button click, could you share why you have that requirement? Is it for accessibility, is it so you can keep keyboard focus in a particular window, is it because you're hoping to run this in an environment (like a deployed application) where the Command Window is not available, or is there some other reason?
Your eval based solution makes me a bit nervous, but if you really want to be able to click on a button you could create a favorite to run that code. To simulate you selecting the text 'x' in your code and copying it to the clipboard I'm using the clipboard function. You would then use eval in conjunction with clipboard('paste') to access the variable. Note that I am not responsible if you were to highlight, for example, some text that when evaluated closes MATLAB or does other nasty things.
x = 1:10;
clipboard('copy', 'x')
plot(eval(clipboard('paste')))
  1 Comment
Paul Hoffrichter
Paul Hoffrichter on 15 Jul 2022
My requirement is for personal convenience. It doesn't matter if i select incorrectly as I will get an error message, and I will correct the selected text.
Thank you @Steven Lord for completing this question with your reference to favorites. I now have a button that runs the code. I just double click a complex variable and now I can hit the favorite button without having to go to the Command Window and entering the p commans. I see that I don't even need the p command anymore since I can just enter @Jan's code directly into the favorite button.
(Now that I know about favorites, I'm sure I'll be adding many more in time.)
Thanks again!

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by