MATLAB Opening Examples and findpeaks() - matlab

I have two issues. First I can't figure out how to properly enter commands openExample('matlab_featured/intro')
http://www.mathworks.com/help/matlab/examples/basic-matrix-operations.html
However, if I go top Help->Example and open up examples from there it works fine.
My second issue is that findpeaks() doesn't seem to work. For instance, the code below causes and error at the findpeaks() command.
data = [25 8 15 5 6 10 10 3 1 20 7]
plot(data)
pks = findpeaks(data)
I'd try opening the example, but I can't find it in the documentation via MATLAB, but I can find it if I go to their site.

The example can be opened just by entering 'intro' in the command
line.
There is a documentation about findpeaks. Just type 'findpeaks' in
the command window and press 'F1' on your keyboard immediately.

Related

Popup value in Simulink Mask doesn't refresh

I am currently masking a block in simulink.
The mask contains a popup list called dbclist with hardcoded type options (1, 2, 3, ..., 7).
The callback function of said popup list looks like this:
msk = Simulink.Mask.get(gcb);
dbcPopup = msk.getParameter('dbclist');
dbcPopup.Value
When changing the value of dbclist while using the mask the command window always responds with:
ans =
1
ans =
1
ans =
1
How can I get the actual value of dbclist?
I am using MATLAB 2014b on Mac OS X.
As stated here (http://de.mathworks.com/matlabcentral/answers/290286-popup-value-in-simulink-mask-doesn-t-refresh) I have found another way to get the actual value of my popuplist. I still don't know what is wrong with the first approach. If anyone figures out where the error is I would really appreciate telling me.

Prepare command in MATLAB

Is there a way in MATLAB to prepare a command programmatically (ie. writing a command directly to the command prompt) so the user can execute it by pressing an enter?
I want to implement my own "Did you mean:" functionality, that is built into MATLAB already.
It can be done using Java from Matlab to programmatically generate key events, along the lines of this answer.
Let's say the command you want to "prepare" is dir. Then
commandwindow; %// make Matlab command window have focus
robot = java.awt.Robot; %/ Java Robot class
robot.keyPress (java.awt.event.KeyEvent.VK_D); %// key press event
robot.keyRelease (java.awt.event.KeyEvent.VK_D); %// key release event
robot.keyPress (java.awt.event.KeyEvent.VK_I);
robot.keyRelease (java.awt.event.KeyEvent.VK_I);
robot.keyPress (java.awt.event.KeyEvent.VK_R);
robot.keyRelease (java.awt.event.KeyEvent.VK_R);
will type dir onto the command window, exactly as if the user had written it. Then pressing Enter will run the command.
The short answer is no. This can't be done as you want it to. What you are trying to do is to write text to MATLAB's stdin and let it remain there unprocessed. Essentially a modified form of piping.
The closest option available in MATLAB is Evaluate Selection where when you select text you can cause MATLAB to execute it in the command prompt. MATLAB puts this text exactly where you want it but it also immediately executes it. There does not seem to be a way to halt this or emulate its functionality programmatically.
Writing to stdin in MATLAB is not allowed as you can see from
>> fprintf(0, 'help conv')
Error using fprintf
Operation is not implemented for requested file identifier.
where 0 denotes stdin, 1 denotes stdout and 2 denotes stderr. Another naive attempt would be to use fopen()
>> fid = fopen(0, 'w');
Error using fopen
Invalid filename.
but alas this fails too. However, we can see from the first attempt that what you desire
is not implemented
The only option to get exactly what you want is that possibly with some MATLAB hackery the ability is there but I'm not aware of it or anybody who has even attempted it.
EDIT: Luis Mendo has provided the MATLAB hackery solution I was talking about.
The closest you could get to what you want is to use hyperlinks to run MATLAB commands like
>> disp('Did you mean: conv()')
Did you mean: conv()
ans =
12
where conv() is a hyperlink and clicking on it will execute conv(a,b) where a = 3; and b = 4; in this example.

How to close one or all currently open Matlab (*.m) files from Matlab command prompt?

I found a solution on the web (see below, circa 2009) which does not work on my machine (Windows 7, Matlab R2013a):
Editor = com.mathworks.mlservices.MLEditorServices;
Editor.closeAll;
As you noticed, newer versions of Matlab do not return the same type of Java object for the editor.
The main editor service can still be accessed with the same command as before:
edtSvc = com.mathworks.mlservices.MLEditorServices ; %// get the main editor service ;
But this only return the handle of the service, not individual editor.
As answered by Daniel, you can close the full service from there, which will close all the editors at once. You can use one of the 2 methods available:
edtSvc.getEditorApplication.close ; %// Close all editor windows. Prompt to save if necessary.
edtSvc.getEditorApplication.closeNoPrompt ; %// Close all editor windows, WITHOUT SAVE!!
Now in this version, each file opened is actually an instance of an editor object. If you want control of individual editor tab/window, you can retrieve the list of the editor objects, then apply methods on them individually:
edtList = edtSvc.getEditorApplication.getOpenEditors.toArray ; %// get a list of all the opened editor
edtList =
java.lang.Object[]:
[com.mathworks.mde.editor.MatlabEditor]
[com.mathworks.mde.editor.MatlabEditor]
[com.mathworks.mde.editor.MatlabEditor]
This return a vector of com.mathworks.mde.editor.MatlabEditor object. (I have 3 opened file in my editor for this example).
From now on, each of these object controls an individual file. You could close a single file already but you need to know which index is the file you want to target. To know which one points to what, you can query the getLongName property:
>> edtList(1).getLongName
ans =
C:\TEMP\StackExchange\Editor_control.m
But if you will have to control individual files, I find it easier to build a structure with field names corresponding to the file names. This could be done this way:
for k=1:length(edtList) ;
[~, fname ]= fileparts( char( edtList(k).getLongName.toString ) ) ;
edt.( fname ) = edtList(k) ;
end
Now I have a structure with meaningful names (well, at least for me, your files and field names will be different of course):
>> edt
edt =
Bending_Movie_Time_Lapse: [1x1 com.mathworks.mde.editor.MatlabEditor]
Editor_control: [1x1 com.mathworks.mde.editor.MatlabEditor]
foldfunction_test: [1x1 com.mathworks.mde.editor.MatlabEditor]
So back to closing an individual file. This can be done easily with one of the same method than before:
edt.foldfunction_test.close %// close with prompt if necessary
edt.foldfunction_test.closeNoPrompt %// close immediately without save
Note that at this stage, you also have access to a nice list of methods and properties for your editor file. You can have a look at them by using the autocompletion (Tab key) of Matlab.
Example done on Matlab R2013a / Windows 7 64 bits
The following seems to work. I've tested in Matlab R2014b, Windows 7 64 bits.
Acccess the editor Java object.
Get the number of open documents, say D.
Programmatically make the editor the window in front.
Programmatically send ALT-F4 keystrokes D times to close all open files. Optionally, send also N keystrokes D times in case some file is not saved and you want to close it (i.e. reply "no" when the editor asks if you want to save it). If the file is already saved, sending an N causes no harm.
For step 1 I found inspiration in this post. For steps 2 and 3 I inspected the methods of the editor object until I found something interesting. For step 4 I took the procedure I used in this answer, which in turn was based on this information.
Code:
closeUnsaved = 1; %// 1 if you want to close even if documentds are not saved
%// Step 1:
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
jEditor = desktop.getGroupContainer('Editor').getTopLevelAncestor; %// editor object
%// Step 2:
D = jEditor.getGroup.getDocumentCount;
%// Step 3:
jEditor.requestFocus; %// make editor the window in front
%// Step 4:
robot = java.awt.Robot;
for n = 1:D
robot.keyPress (java.awt.event.KeyEvent.VK_ALT); %// press "ALT"
robot.keyPress (java.awt.event.KeyEvent.VK_F4); %// press "F4"
robot.keyRelease (java.awt.event.KeyEvent.VK_F4); %// release "F4"
robot.keyRelease (java.awt.event.KeyEvent.VK_ALT); %// release "ALT"
if closeUnsaved
robot.keyPress (java.awt.event.KeyEvent.VK_N); %// press "N"
robot.keyRelease (java.awt.event.KeyEvent.VK_N); %// release "N"
end
end

Is there a way to automatically suppress Matlab from printing big matrices in command window?

Is there an option in matlab or a plugin/app or a trick such that if you are in an interactive command session, every time it would print out a matrix way too big for a human to look through, it redacts the output to either a warning of how big the matrix is or a summary (only a few rows and columns) of the matrix?
There are many times where I want to examine a matrix in the command window, but I didn't realize how big it was, so I accidentally printed the whole thing out. Or some place inside a function I did not code myself, someone missed a semicolon and I handed it a big matrix, and it dumps the whole thing in my command window.
It make sense that in 99.99% of the time, people do not intend to print a million row matrix in their interactive command window, right? It completely spams their scroll buffer and removes all useful information that you had on screen before.
So it makes much more sense for matlab to automatically assume that the user in interactive sessions want to output a summary of a big matrix, instead of dumping the whole thing into the command window. There should at least be such an option in the settings.
One possibility is to overload the display function, which is called automatically when you enter an expression that is not terminated by ;. For example, if you put the following function in a folder called "#double" anywhere on your MATLAB path, the default display behavior will be overridden for double arrays (this is based on Mohsen Nosratinia's display.m for displaying matrix dimensions):
% #double/display.m
function display(v)
% DISPLAY Display a variable, limiting the number of elements shown.
name = inputname(1);
if isempty(name)
name = 'ans';
end
maxElementsShown = 500;
newlines = repmat('\n',1,~strcmp(get(0,'FormatSpacing'),'compact'));
if numel(v)>maxElementsShown,
warning('display:varTooLong','Data not displayed because of length.');
% OR show the first N=maxElementsShown elements
% builtin('disp', v(1:maxElementsShown));
elseif numel(v)>0,
fprintf([newlines '%s = \n' newlines], name);
builtin('disp', v);
end
end
For example,
>> xx=1:10
xx =
1 2 3 4 5 6 7 8 9 10
>> xx=1:1e4
Warning: Data not displayed because of length.
> In double.display at 17
EDIT: Updated to respect 'compact' and 'loose' output format preference.
EDIT 2: Prevent displaying an empty array. This makes whos and other commands avoid an unnecessary display.

How to make output of Matlab m file to stay for a while

I have matlab m file which plots 3 graphs using subplot. Now i also have a UNIX script which invokes this script and passes the function name as parameter.
I have two problems:
I am getting the warning Type-ahead buffer Overflow
The plot remains only for a few seconds before disappearing. How can I keep plot active til l user clicks on cross button?
thanks!
It's hard to answer your first question without some code.
You can use the pause command to wait for the user to press any key.