I would like to use these answer to insert the char % at the end of each line in a marked region when using emacs. However, in order to avoid endings like %% I have to first delete all the occurrences of % at the end of the lines in the region. For example
foo%
foo2
foo3%
foo4%%
bar
bar%
should become
foo
foo2
foo3
foo4
bar
bar
Note that not all lines in the region end with % (otherwise I would be done), and some might end with more the one %. That is, one cannot simply delete the last char of each line. I guess it is rather simple, but I'm too much of emacs newbie.
C-M-%%+C-qC-jEnterC-qC-j! invokes query-replace-regexp replaceing any number of %'s followed by a newline by a newline.
You can directly do the two steps in one, replacing any existing % characters at end of line with only one.
C-M-% <- query-replace-regexp
%*$ <- 0 or more %s at end of line
Enter
% <- by only one %
Enter
! <- apply to all (optional, see below)
If you are using Transient Mark mode, then the command will operate inside the active region. Otherwise, you may choose to narrow-to-region (C-x n n)to make sure it doesen't affect other parts of the file.
Related
Based on MATLAB's code for strtok (see end):
"Here’s a more advanced example that finds the first token in a character string. A token is a set of characters delimited by whitespace or some other character. Given one input, the function assumes a default delimiter of whitespace; given two, it lets you specify another delimiter if desired. It also allows for two possible output argument lists"
I have a few questions:
1) Is a delimiter specified at the beginning or end of a token?
So for example, if I wanted to find the section of a text which gave me a certain date and the whole text was: "I like the date april 10 because it is close to May Day". I imagine the token is "april 10" but the starting delimiter would be "a" and the ending delimiter would be a digit?
You see I am confused as to what a "delimiter" is exactly in context. In MATLAB I would normally probably write the token as (\w*\s\d*) in order to locate the date (april 10) in the text since I do not know what date it would be (what letter it starts with or the digits after it). But is a delimiter that whole "april 10" or just an "a" at the beginning? How would this help if I do not know what month it is (april, may, june, etc) or does it basically just work as a "find" command?
I ran the program shown in the picture and tried it with 'hello my friend' as the string and 'o' as the delimiter and it gives:
token=hell
remainder=o my friend
So basically I am getting the impression delimiter are usually used at the end of fields or different regions in order to specify when the new field/section (remainder) begins? Basically a delimiter is commonly used as a simple one (or maybe more) character device to indicate the start of a new field or datum whereas using (/d/w*....etc) format is used for more specific extractions like dates where there is no "comma" or specific indicator in front of it? Are these two observations correct?
BUT then when I run it using "hello my fri" as delimiter (see --> running it with delimiter, it seems to arbitrarily select "I want to say hello my friend good man" as the remainder and "nd" as the token which makes no sense so I am wondering if there is a bug in this program or if it's just not set up to handle a delimiter that appear twice.
Also,
2) Can someone please explain why [9:13 32] is made the default for one input argument? If we're assuming whitespace is the delimiter, then what does that [9:13 32] mean?
3) Is there any purpose to using "any" since it is ran by a looping process? Would not it check it each iteration anyways?
function [token, remainder] = strtok(string, delimiters)
%STRTOK Find token in string.
% TOKEN = STRTOK(STR) returns the first token in the string STR delimited
% by white-space characters. STRTOK ignores any leading white space.
% If STR is a cell array of strings, TOKEN is a cell array of tokens.
%
% TOKEN = STRTOK(STR,DELIM) returns the first token delimited by one of
% the characters in DELIM. STRTOK ignores any leading delimiters.
% Do not use escape sequences as delimiters. For example, use char(9)
% rather than '\t' for tab.
%
% [TOKEN,REMAIN] = STRTOK(...) returns the remainder of the original
% string.
%
% If the body of the input string does not contain any delimiter
% characters, STRTOK returns the entire string in TOKEN (excluding any
% leading delimiter characters), and REMAIN contains an empty string.
%
% Example:
%
% s = ' This is a simple example.';
% [token, remain] = strtok(s)
%
% returns
%
% token =
% This
% remain =
% is a simple example.
%
% See also ISSPACE, STRFIND, STRNCMP, STRCMP, TEXTSCAN.
% Copyright 1984-2009 The MathWorks, Inc.
if nargin<1
error(message('MATLAB:strtok:NrInputArguments'));
end
token = ''; remainder = '';
len = length(string);
if len == 0
return
end
if (nargin == 1)
delimiters = [9:13 32]; % White space characters
end
i = 1;
while (any(string(i) == delimiters))
i = i + 1;
if (i > len),
return,
end
end
start = i;
while (~any(string(i) == delimiters))
i = i + 1;
if (i > len),
break,
end
end
finish = i - 1;
token = string(start:finish);
if (nargout == 2)
remainder = string(finish + 1:length(string));
end
EDIT: I was not aware that strtok was a built in function. I was under the assumption it was a UDF the textbook was building as an example. This is why there are many ambiguities since the book does not specify clearly what the function does.
This, for example, was not specified in the text which only stated the function found the first token in a character string. --> token = strtok(str) parses input character vector str from left to right, returning part or all of that character vector in token. Using the white-space character as a delimiter, the token output begins at the start of str, skipping any delimiters that might appear at the start, and includes all characters up to either the next delimiter or the end of the character vector. White-space characters include space (ASCII 32), tab (ASCII 9), and carriage return (ASCII 13).
Copyright 1984-2009 The MathWorks, Inc.
strtok is very much not going to help you here so I'm not going to answer your main question. I think you should use regular expression for this but I don't speak regex so I'll leave that to someone else.
[9:13 32]
Why is the default delimiter set to [9:13 32]. From the comments, MATLAB is claiming that those are all the white space characters. In other words then numbers 9, 10, 11, 12, 13 and 32 are the ASCII values for white space characters. For example 32 is the value of a space. Prove this to yourself by casting one to an integer:
uint8(' ') % or even ' ' + 0
I don't know what all the others are but I'm pretty sure one must be the tab character. To check the ASCII value of a tab character you can do
uint8(sprintf('\t'))
which returns 9 which is indeed in the list.
So [9:13 32] is a list of all the white space characters, as the comment implies.
Actually there are many more white space characters that this doesn't cover: https://en.wikipedia.org/wiki/Whitespace_character
any
When you say any I'm assuming you mean in lines like this: any(string(i) == delimiters). So yes, the loop ensures that only one character of string is compared at a time however there can be multiple values in delimiter for example all the white space characters as mentioned above or maybe you called strtok like this:
strtok('I like the date...', 'ad')
now both 'a' and 'd' are used as delimiters and so it returns
'I like the '
because it hit a 'd' first.
Is it possible to add a customized dbstop condition to Matlab?
Recently I found myself with out of bounds values in multiple variables, one way to track down the first occurance of this would be to set a conditional breakpoint on each line where these values are updated. However, I hope there is an easier way to do this.
I have recently had to track down a NaN which was fairly trivial due to:
dbstop if naninf
Hence I hope that it is possible to get something like:
dbstop if anything outside myBound
or
dbstop if myVariable outside myBound
I would of course be willing to take the performance hit that one may expect.
If you use the editor, you can set a stop as normal, right-click on it, select "set/modify condition" and enter the condition (the stop will turn from red to yellow).
From command line, you can use
dbstop in file if expression
dbstop in file at location if expression
e.g.
dbstop in myFile at 200 if (~isempty(var) && var > 3)
as mentioned by #LuisMendo.
The second option may be more useful, since the first one seems to be only evaluated at the start of the file. In other words, it doesn't seem to be possible to have a similarly generic expression as dbstop if naninf that checks for certain values across an entire file.
The problem with using the form "DBSTOP in FILESPEC if EXPRESSION" of dbstop is that it sets a breakpoint only at the first line of the file. A solution is to use the form "DBSTOP in FILESPEC at LINENO if EXPRESSION" to set a breakpoint at each line.
Consider the following example script, saved on a file called testfile.m.
clear all
for m = 1:10;
k = 2*m
end
Say we want to stop if variable k exceeds the value 6. We first automatically set the breakpoints in all lines of this file:
file = 'testfile.m';
varname = 'k';
expression = 'k>6'; %// it should be 'exist(''k'')&&k>6', but that's added later
%// Determine number of lines of file:
fid = fopen('testfile.m');
cont = 1;
nlines = 0;
while cont
readline = fgetl(fid);
cont = ~isequal(readline,-1);
nlines = nlines + cont;
end
fclose(fid);
%// Set breakpoint at each line. We need eval for this
for n = 1:nlines
eval(['dbstop in ' file ' at ' num2str(n) ' if ( exist(''' varname...
''') && ( ' expression ' ) )'])
end
Now, after running the above (check that every line of testfile.m has a yellow breakpoint), run testfile and check values when it stops:
This is admittedly a little cumbersome if you have several variables or files. Also, I'm not sure how many simultaneous breakpoints Matlab supports (we are using one for each program line).
Thinking outside the box - you could write a class to hold your variable. There you could have a customized setter that will raise a warning if you violate boundaries. dbstop if warning should then be enough.
Is it possible to add a customized dbstop condition to Matlab?
Recently I found myself with out of bounds values in multiple variables, one way to track down the first occurance of this would be to set a conditional breakpoint on each line where these values are updated. However, I hope there is an easier way to do this.
I have recently had to track down a NaN which was fairly trivial due to:
dbstop if naninf
Hence I hope that it is possible to get something like:
dbstop if anything outside myBound
or
dbstop if myVariable outside myBound
I would of course be willing to take the performance hit that one may expect.
If you use the editor, you can set a stop as normal, right-click on it, select "set/modify condition" and enter the condition (the stop will turn from red to yellow).
From command line, you can use
dbstop in file if expression
dbstop in file at location if expression
e.g.
dbstop in myFile at 200 if (~isempty(var) && var > 3)
as mentioned by #LuisMendo.
The second option may be more useful, since the first one seems to be only evaluated at the start of the file. In other words, it doesn't seem to be possible to have a similarly generic expression as dbstop if naninf that checks for certain values across an entire file.
The problem with using the form "DBSTOP in FILESPEC if EXPRESSION" of dbstop is that it sets a breakpoint only at the first line of the file. A solution is to use the form "DBSTOP in FILESPEC at LINENO if EXPRESSION" to set a breakpoint at each line.
Consider the following example script, saved on a file called testfile.m.
clear all
for m = 1:10;
k = 2*m
end
Say we want to stop if variable k exceeds the value 6. We first automatically set the breakpoints in all lines of this file:
file = 'testfile.m';
varname = 'k';
expression = 'k>6'; %// it should be 'exist(''k'')&&k>6', but that's added later
%// Determine number of lines of file:
fid = fopen('testfile.m');
cont = 1;
nlines = 0;
while cont
readline = fgetl(fid);
cont = ~isequal(readline,-1);
nlines = nlines + cont;
end
fclose(fid);
%// Set breakpoint at each line. We need eval for this
for n = 1:nlines
eval(['dbstop in ' file ' at ' num2str(n) ' if ( exist(''' varname...
''') && ( ' expression ' ) )'])
end
Now, after running the above (check that every line of testfile.m has a yellow breakpoint), run testfile and check values when it stops:
This is admittedly a little cumbersome if you have several variables or files. Also, I'm not sure how many simultaneous breakpoints Matlab supports (we are using one for each program line).
Thinking outside the box - you could write a class to hold your variable. There you could have a customized setter that will raise a warning if you violate boundaries. dbstop if warning should then be enough.
The MATLAB editor automatically highlights all content after %% comments, and text after %% in the same line are turned bold. But what's the essential difference here? Why do people sometimes use %% instead of %?
From a syntax point of view, they are both comments.
In the Matlab editor, Matlab parses %% delimited blocks as "sections" which you can run as a unit independent of running the whole script.
One percent sign (%) is used for commenting lines.
Two percent signs (%%) have a different purpose: they are used for dividing your code into sections, which can be run independently. This allows easier debugging.
I really like the double percent sign (%%) and use as far as possible for the following reasons:
Creates a cell block which could be run separately from the whole code (Ctrl + Enter).
As mentioned in sections, it improves the readability of the file and appears as a heading if you publish your code. It increases concentration by creating a yellow background and you can focus more on the part that you are working on.
You can fold the code in cell blocks. (First you should enable code folding of cell blocks in Preferences >> Editor/Debugger >> Code Folding >> Sections). This is useful specially in large mfiles.
If you care about keeping a clean Command History running the codes in cell blocks (Ctrl + Enter) does not leave any trace in Command History , unlike the Evaluate Selection (F9) which evaluates the selected (highlighted) code and holds the executed code in Command History.
Hope it helps.
I'm curious about the progress of the running program and I print some information about the current iteration such as:
for i = 1:N
...
...
msg = sprintf('Processed %d/%d', i, N);
display(msg)
end
I don't want to print the progress on separate lines, instead, I want the last line to replace the previous one. I don't want to use clc which clears all the content.
I know that '\b' can clear the last character (like backspace) and I can create a function with a for loop which clears the items till the previous new line before the last. But is there a better way to do that? If not, how can I check whether the last character on the command line is a new line or not?
I've looked at the problem, a while ago. And I've noticed that the character \r (used to erase the last line) works with matlab in command-line (-nodesktop) but not with the graphic mode...
The best solution I found is to do something like that:
n=0;
for ...
...
fprintf(repmat('\b',1,n));
fprintf(msg);
n=numel(msg);
end
Yair Altman has a very nice example on his blog of how you can use the backspace control-character (\b) to do what you want but in an easier way than you were considering. Modifying your code to resemble his example, you could do something like this:
reverseStr = '';
for i = 1:N
...
...
msg = sprintf('Processed %d/%d', i, N);
fprintf([reverseStr, msg]);
reverseStr = repmat(sprintf('\b'), 1, length(msg));
end
I use 'dispstat' function just for this purpose. It can update the previous output which is a missing function of default 'disp'. Very simple to use. It can be downloaded from here: http://www.mathworks.com/matlabcentral/fileexchange/44673-overwritable-message-outputs-to-commandline-window
***Sample usage:
dispstat('','init'); % One time only initialization
dispstat(sprintf('Begining the process...'),'keepthis','timestamp');
for i = 97:100
dispstat(sprintf('Progress %d%%',i),'timestamp');
%doing some heavy stuff here
end
dispstat('Finished.','keepprev');
***Output:
11:25:37 Begining the process...
11:25:37 Progress 100%
Finished.
All the best
Is this about what you are looking for
%# create title
fprintf('processed: %03d',0)
for i=1:10
%# delete last three digit number and replace with new
%# loop index
fprintf('\b\b\b\b %03d',i);
%# process here
pause(.5)
end
%# clear line
fprintf('\n');
But if your code displays other results this won't work. and you might want to consider using a message box to update progress.
Another solution that overwrites the entire previous line relies on the \r formatting character,
ctrl=0;
while ctrl<5
fprintf('\rctrl: %i',ctrl);
ctrl=ctrl+1;
pause(2); % To highlight overwrite
end
fprintf('\n'); % Don't forget the newline