How to create Plot button for Complex vector?

2 vues (au cours des 30 derniers jours)
Paul Hoffrichter
Paul Hoffrichter le 10 Juin 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.
  6 commentaires
Jan
Jan le 12 Juin 2022
Where should this button appear?
Paul Hoffrichter
Paul Hoffrichter le 13 Juin 2022
Modifié(e) : Paul Hoffrichter le 13 Juin 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.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 13 Juin 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 commentaires
Jan
Jan le 15 Juil 2022
I'm still convinced, that trying to modify Matlab's editor GUI will not be a clean solution. Then the editor becomes a multi-purpose tool, also known as "god tool" which is a famous programming anti-pattern.
Paul Hoffrichter
Paul Hoffrichter le 15 Juil 2022
Modifié(e) : Paul Hoffrichter le 15 Juil 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.

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 15 Juil 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')))
  2 commentaires
Paul Hoffrichter
Paul Hoffrichter le 15 Juil 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!
Paul Hoffrichter
Paul Hoffrichter le 9 Nov 2022
Modifié(e) : Paul Hoffrichter le 9 Nov 2022
I just wanted to thank both of you again for your help. My "plot" and "fft-plot" favorite buttons are a great time-saver, and helps a lot when I work with co-workers. Just last week a co-worker asked for help in code that I wasn't that familiar with. As we stepped through the problem area, I was quickly able to see how the code was transforming the data by hitting the Plot and FFT-Plot favorites. Granted, I could have asked to just type in plot commands on expressions, but was so much easier to say, highlight that variable or expression and hit the Plot and then the FFT-Plot favorite button. Without it, I would say highlight the expression, copy it, paste it into the command window, and surround the expression with a Plot or FFT-plot function.
I even made a couple of enhancements:
  1. For matrix of vectors, if special variable, 'N_', exists, then the favorite uses it for the 2nd dimension; otherwise, plot the overlapping vectors.
  2. Added isreal test to plot accordingly.
  3. I sometimes want to plot what is in the command window. So I highlight the variable or expression, hit CTRL/C (probably a simpler way), but, this seems to work fine:
if ~isempty( activeEditor.SelectedText )
Str = activeEditor.SelectedText;
else
Str = clipboard('paste');
end
Thanks again,
- Paul

Connectez-vous pour commenter.

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by