How to create Plot button for Complex vector?

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 个评论

@Paul Hoffrichter - so you would like to add your function to the plot catalog? i.e. that list of plots that appear when you select the variable and bring up the menu?
@Geoff Hayes, I have not heard of the term "plot catalog". At the top of the MATLAB GUI are tabs: HOME, PLOTS, APPS, EDITOR, PUBLISH, and VIEW. If I select the PLOTS tab, my favorite plot buttons show up:
  • plot
  • semilogy
  • stem
  • wvtool
  • and more.
If I highlight a vector in the Workspace and hit one of those buttons, a figure appears showing a plot of that vector. I would like to add a plot button, say, plotC, which would plot a complex vector as if I entered:
>> PTRIn = @(x) plot(1:length(x), real(x), '-b', 1:length(x), imag(x),'--g');
>> PTRIn( myComplexVector);
I also have a .m file that takes in a complex variable and produces a different kind of figure. Is that also doable. (It can also take an optional second sampling rate argument, but guessing that would be hard to do with a button by highlighting in the Workspace both the vector and the scalar sampling rate variable.)
If there is any other approach which eliminates the need for me to add the PTRIn(x) function to each function, i would appreciate that technique if that makes it easier.
I like the button idea, but I do not mind typing in PTRIn(myvariable). I just don't want to be editing my code all the time by adding this function (or even adding global functions).
@Paul Hoffrichter- I don't think you can add custom plot functions to the plot "catalog" (whether you access this from the menu PLOTS tab or elsewhere. I guess I don't understand what you mean by 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. What are the other functions?
Where should this button appear?
>> 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.

请先登录,再进行评论。

 采纳的回答

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 个评论

Is there any way to get a button (placed anywhere) that accepts a highlighted vector (either in the editor or the workspace), to plot the variable?
Yes, this is possible, but really tricky. Note that highlighting "x" in the editor does not allow to guess, which value x has. This will work only, if you stop the code using the debugger. Then you use the debugger and the editor as a kind of GUI to display graphics.
The workspace is harder: I do not know how to request the currently highlighted variable. Or do you mean the CommandWindow?
I'd avoid such Meta-GUIs consequently. Either I use the command window and call the function using a smart function name like "riplot". Or I create a GUI to display the data. Mixing editing, debugging, current worksapce variables and graphic output is not a well defined way to produce outputs, but a kind of hacking.
>> Or do you mean the CommandWindow?
Not CommandWindow. I meant the highlighting an item in the workspace (as we do for the PLOT, STEM, etc. buttons). Or, alternatively, by highlighting a complex vector in the Matlab editor's window.
>> This will work only, if you stop the code using the debugger.
Yes, I am always in a stopped debugger when I use the PLOT, STEM buttons, or using PTRIn function.
>> Yes, this is possible
>> use the debugger and the editor as a kind of GUI to display graphics
That sounds just like what I was looking for. I can highlight a complex vector in the editor, and press a button (or maybe a mouse click?) and get a figure. Very nice.
"I am always in a stopped debugger when I use the PLOT, STEM buttons, or using PTRIn function" - and this is a complicated method to use Matlab. Of course you can hit a nail in the wall using the backside of a drilling machine, but this is not elegant.
Maybe these methods help you:
% !!!UNTESTED CODE!!!
activeEditor = matlab.desktop.editor.getActive;
Text = activeEditor.Text;
selectionPosition = activeEditor.Selection;
Str = Text(selectionPosition(1):selectionPosition(2));
x = eval(Str); % DANGEROUS! Test with WHO if this is a variable at first
plot(1:length(x), real(x), '-b', 1:length(x), imag(x),'--g');
I cannot test this currently. Maybe the idea helps.
  • Thanks for these ideas. This appears to be the correct syntax:
activeEditor = matlab.desktop.editor.getActive;
Str = activeEditor.SelectedText;
x = eval(Str);
figure(111),PTRIn(x); title(Str)
  • Is there a way to create a button to execute the above?
  • "I am always in a stopped debugger when I use the PLOT button" - and this is a complicated method to use Matlab.
Please advise what approach you take that simplifies life. Here is the picture of the plot button I hit after I select a vector in the workspace.
I don't know how to get a good plot of my vector that is not changing if the program is running. I look at the plot while on a breakpoint and if I don't like what I see; I change the code and either start over or just run the Section again.
I was hoping to get a button in the question, but since no one offered a button solution,
"How to create Plot button for Complex vector?"
I'll have to assume that it is impossible to get the button, and accept this answer.
If someone does come up with a belated solution, I would appreicate that.
Thanks. I liked learning about the matlab.desktop.editor.getActive feature.
@Paul Hoffrichter: "I don't know how to get a good plot of my vector that is not changing if the program is running" - the usual solution is to write code, which produces what you want. So asking for a specific method, which does not change the program is the opposite of the idea programming languages are designed for.
I cannot decide where the wanted button should appear. You said "anywhere", but this is a keyword which let me tend to leave a discussion.
If an asnwer does not satisfy your needs, do not accept an answer. Open questions attract more readers. Feel free to unaccept this answer.
I am only looking at the data when I have stopped in the debugger.
=============
>> Is there any way to get a button
>> Yes, this is possible, but really tricky. Note that highlighting "x" in the editor does not allow to guess, which value x has. This will work only, if you stop the code using the debugger. Then you use the debugger and the editor as a kind of GUI to display graphics.
=============
The solution you provided will be a good step forward so no need to unaccept. (I will ask another pointed question about buttons if need be.) You refer to a tricky button solution. I don't want the "anywhere" placement to be an issue as long as the button is visible after I hit a breakpoint - this is used for my own personal debugging, so nothing fancy is required. If you could provide the steps to get a button, that would be nice. I do recall watching a Loren Matlab video where she had customized buttons just above current folder text field above the editor.
=============
>> "I am always in a stopped debugger when I use the PLOT, STEM buttons, or using PTRIn function" - and this is a complicated method to use Matlab
To clarify.. I also have programs running and when they finish, I have a set of figures that I can look at. But when I need to examine a specific figure in depth for debugging, I hit a breakpoint, and start testing other values and other code manually and get corresponding figures until I figure out what changes should be made to the code. Then I run the code without breaks to see if the figures are not good. If this a wasteful "complicated method to use Matlab", then please let me know via discussion or tutorial links how I should be debugging using Matlab in a way that is less complicated.
Thank you,
Paul
The code I wrote based on your ideas work when I copy/paste it into the Command Window. But when I make a script out of it, then I still see the variable, but I guess since the variable is not highlighted in the script, then the line
Str = activeEditor.SelectedText;
results in Str being '' instead of the highlighted variable. Any idea how to overcome this issue?
Please post the relevant part of the code. What does "highlighted" mean? Why do you care about highlighting in the editor? Does the code work?
Apologies. Yesterday at the office, Str was '', so no plot. Not sure what went wrong there. Today, at home, I get a plot with the following P.m script:
activeEditor = matlab.desktop.editor.getActive;
Str = activeEditor.SelectedText;
x = eval(Str);
figure, plot(1:length(x), real(x), '-b', 1:length(x), imag(x),'--g');
In the editor, I double-click on a complex vector to highlight it, and then type P in the command window, and I get my plot. Tested this in R2020a and R2021b.
Thank you!
Original goal is to define a button in the editor or in the tool strip the would execute the P script. Do you know if that is possible?
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.
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.

请先登录,再进行评论。

更多回答(1 个)

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 个评论

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!
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

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 App Building 的更多信息

产品

版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by