How to create Plot button for Complex vector?
1 view (last 30 days)
Show older comments
Paul Hoffrichter on 10 Jun 2022
Edited: Paul Hoffrichter on 9 Nov 2022
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.
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.
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:
plot(1:length(x), real(x), '-b', 1:length(x), imag(x),'--g');
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.
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.
More Answers (1)
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;
Find more on Debugging and Analysis 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!